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

2023年 9月 17日 75.8k 0

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

假设您想使用Python CGi脚本登录您的帐户,以下是详细信息

login.html

登录后复制

登录.py

#!C:Python27python.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('''

Hello Word - First CGI Program

successfully login

''')
else:
# Commit your changes in the database
db.commit()
print("Content-type: text/html")
print("")
print("")
print("

fail

")
print("")
print("")
except:
# Rollback in case there is any error
db.rollback()

# disconnect from server
db.close()

登录后复制

以上就是如何编写Python CGI程序与MySQL交互?的详细内容,更多请关注每日运维网(www.mryunwei.com)其它相关文章!

相关文章

Oracle如何使用授予和撤销权限的语法和示例
Awesome Project: 探索 MatrixOrigin 云原生分布式数据库
下载丨66页PDF,云和恩墨技术通讯(2024年7月刊)
社区版oceanbase安装
Oracle 导出CSV工具-sqluldr2
ETL数据集成丨快速将MySQL数据迁移至Doris数据库

发布评论