
假设您想使用Python CGi脚本登录您的帐户,以下是详细信息
login.html
这本书假定你没有任何关于脚本或一般程序的编程知识, 但是如果你具备相关的知识, 那么你将很容易就能够达到中高级的水平. . . 所有这些只是UNIX®浩瀚知识的一小部分. 你可以把本书作为教材, 自学手册, 或者是关于shell脚本技术的文档. 书中的练习和样例脚本中的注释将会与读者进行更好的互动, 但是最关键的前提是: 想真正学习脚本编程的唯一途径就是亲自动手编写脚本. 这本书也可作为教材来讲解一般的编程概念. 向伟大的中华民族的Linux用户致意! 我希望这本书能够帮助你们学习和理解L
20
<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中文网其它相关文章!
python怎么学习?python怎么入门?python在哪学?python怎么学才快?不用担心,这里为大家提供了python速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号