【问题标题】:accessing a database from a python cgi从 python cgi 访问数据库
【发布时间】:2013-06-10 04:01:02
【问题描述】:

我将 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 新手。那么你们能帮帮我吗? 蟒蛇版本-2.7 数据库-mysql 服务器-apache 2.2 win32位

【问题讨论】:

  • 你可能正在得到一个错误,但你不会在输出中看到它,因为它最终出现在 apache 的日志中,所以检查你的日志,或者添加 cgitb: import cgitb; cgitb.enable()
  • 您可能不想以 root 身份连接,尝试创建一个没有删除整个数据库权限的用户帐户。将管理员模式用作用户模式绝不是最好的主意。
  • 当我尝试使用 python 连接到数据库时,我做对了......但在 python cgi 中它不起作用......

标签: python mysql apache


【解决方案1】:

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

import cgitb; cgitb.enable()

【讨论】:

  • 是的...它显示一个错误,..内部服务器错误..你能告诉你这个错误是否出现,代码有什么错误吗?
  • 页面不显示回溯吗?
  • 如果不回溯,请在命令行中尝试 cgi 脚本:echo t1=blah | python cgi_script.py
  • yaa..i 确实添加了 #!/python27/python/ 忘记在查询中添加...对不起
  • 嘿..我没有得到输出...帮我解决问题,因为我被困在一个项目的中间
猜你喜欢
  • 1970-01-01
  • 2010-10-11
  • 1970-01-01
  • 2013-04-14
  • 2018-02-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-11
相关资源
最近更新 更多