python - 从 python cgi 访问数据库

标签 python mysql apache

我将 httpd 文件配置为运行网站中给出的 python 脚本。配置完成后,我很惊讶地看到 .py 文件被放置在 htdocs 文件夹中时被执行,但是 .cgi 文件没有被执行。该错误表示内部错误。 想继续使用 .py 文件,但是当我尝试访问 mysql 数据库时,我无法访问。 我的 .py 文件是:

import cgi
import MySQLdb

print "Content-type: text/html"
print
print "<html><head>"
print ""
print "</head><body>"
form=cgi.FieldStorage()
name=form["t1"].value
print "Hello. %s" %name
print "hai"
print '<input type="submit"/>'
Con = MySQLdb.Connect(host="127.0.0.1", port=3306, user="root", passwd="pwd", db="db1")
cursor = Con.cursor()
sql="SELECT * FROM rec"
cursor.execute(sql)
data = cursor.fetchone()
print "%s" %data
print "</body></html>"

我没有收到任何错误,但是没有打印“数据” 我得到的输出是:

hello name hai submit button

Python 新手。那么你们能帮帮我吗? python 版本-2.7 数据库-mysql 服务器-apache 2.2 win32位

最佳答案

将下一行放在第一行,看看发生了什么错误(回溯)。

import cgitb; cgitb.enable()

关于python - 从 python cgi 访问数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17092638/

相关文章:

python - 是否可以恢复迭代器并可以分配其值/状态?

python - 无法将 azure.cognitiveservices.search.web 搜索模块导入程序

mysql - #1139 - 从正则表达式中得到错误 'repetition-operator operand invalid'

regex - Apache 使用 RewriteCond Regex 给出错误 500

django - 无法在运行 Ubuntu 14.04 的 EC2 实例上在生产环境中设置 Django

python - 如何转义字符串中的某些字符?

python - 异常时启动 IPython shell

mysql - 如何从一个 mysql::Conn 创建多个准备好的语句?

php - Mysql 获取记录常见记录以及不常见记录

php - 使用 dockerfile 在 docker 中安装 mongodb 驱动程序