您好,欢迎访问一九零五行业门户网

如何编写Python CGI程序与MySQL交互?

假设您想使用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.exeimport mysqldbimport cgiimport cookie# open database connectiondb = mysqldb.connect(localhost,root,,student )# prepare a cursor object using cursor() methodcursor = 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 commandif(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交互?的详细内容。
其它类似信息

推荐信息