假设您想使用python CGi脚本登录您的帐户,以下是详细信息 login.html <html> <body> <fORM action="login.py" method="get"&
假设您想使用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()
--结束END--
本文标题: 如何编写Python CGI程序与MySQL交互?
本文链接: https://www.lsjlt.com/news/438553.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-05-16
2024-05-16
2024-05-16
2024-05-15
2024-05-15
2024-05-15
2024-05-15
2024-05-15
2024-05-15
2024-05-15
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0