如何寫Python CGI程式與MySQL互動?
Sep 17, 2023 pm 09:01 PM假設您想要使用Python CGi腳本登入您的帳戶,以下是詳細資訊
login.html
#<html> <body> <form action="login.py" method="get"> email: <input type="text" name="e1"> password: <input type="password" name="p1"> <input type="submit" value="register"> </form> </body> </html>
登入後複製
登入.py
##!C:\Python27\python.exe import MySQLdb import cgi import Cookie # Open database connection db = MySQLdb.connect("localhost","root","","student" ) # prepare a cursor object using cursor() method cursor = db.cursor() data=cgi.FieldStorage() a=data.getvalue('e1') b=data.getvalue('p1') # Prepare SQL query to fetch a record into the database. sql = "select id,email,password from user where email='"+a+"' AND password='"+b+"'" try: # Execute the SQL command if(cursor.execute(sql)): # Commit your changes in the database db.commit() c=Cookie.SimpleCookie() # assign a value c['mou']=a # set the xpires time c['mou']['expires']=24*60*60 # print the header, starting with the cookie print c print("Content-type: text/html") print('''<html> <head> <title>Hello Word - First CGI Program</title> </head> <body> <h2>successfully login</h2> </body> </html>''') else: # Commit your changes in the database db.commit() print("Content-type: text/html") print("<html>") print("<body>") print("<h2>fail</h2>") print("</body>") print("</html>") except: # Rollback in case there is any error db.rollback() # disconnect from server db.close()
登入後複製
以上是如何寫Python CGI程式與MySQL互動?的詳細內容。更多資訊請關注PHP中文網其他相關文章!
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱門文章
R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
2 週前
By 尊渡假赌尊渡假赌尊渡假赌
倉庫:如何復興隊友
4 週前
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前
By 尊渡假赌尊渡假赌尊渡假赌
擊敗分裂小說需要多長時間?
3 週前
By DDD
R.E.P.O.保存文件位置:在哪里以及如何保護它?
3 週前
By DDD

熱門文章
R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
2 週前
By 尊渡假赌尊渡假赌尊渡假赌
倉庫:如何復興隊友
4 週前
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前
By 尊渡假赌尊渡假赌尊渡假赌
擊敗分裂小說需要多長時間?
3 週前
By DDD
R.E.P.O.保存文件位置:在哪里以及如何保護它?
3 週前
By DDD

熱門文章標籤

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

在 Linux 中運行 MySQl(有/沒有帶有 phpmyadmin 的 podman 容器)

哪些流行的MySQL GUI工具(例如MySQL Workbench,PhpMyAdmin)是什麼?
