【发布时间】:2010-10-11 22:26:05
【问题描述】:
#!/usr/bin/env python
import MySQLdb
print "Content-Type: text/html"
print
print "<html><head><title>Books</title></head>"
print "<body>" print "<h1>Books</h1>"
print "<ul>"
connection = MySQLdb.connect(user='me', passwd='letmein', db='my_db') cursor = connection.cursor() cursor.execute(“SELECT name FROM books ORDER BY pub_date DESC LIMIT 10”)
for row in cursor.fetchall():
print "<li>%s</li>" % row[0]
print "</ul>"
print "</body></html>"
connection.close()
我将它作为 test.cgi 保存到我的网络服务器。我通过 www.mysite.com/test.cgi 运行它没有成功
Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
你如何解决这个问题?
[编辑]在第一个答案之后
- test.cgi 是可执行的(我运行 $ chmod +x test.cgi)
- 我使用 Apache。
- 我在 .bashrc 中有这个 export PATH=${PATH}:~/bin
- Python 模块 MySQLdb 已安装。
- 代码没有智能引号。
[编辑]第二个答案后
您收到该错误是因为您 尚未安装 MySQLdb 模块 Python 需要与 MySQL 对话 数据库
我在我的系统上安装了 MySQLdb。该模块有效,因为我可以导入它们。 但是,当我访问 www.[mysite].com/test.cgi 时,我仍然遇到同样的错误。
[编辑]
我不确定这些问题
connect() 参数是否正确? MySQL 是否在 localhost 上运行 在默认端口?
我在我的服务器上运行 MySQL。关于 connect() 参数的问题与这里有关吗?
SELECT 语句是否正确?
你的意思是我的SQL语句比如SELECT语句是否正确? 我还没有使用任何 SQL 查询。 我在这里需要它们吗?
【问题讨论】:
-
很高兴您修复了“智能引号”。这里有一些关于我刚刚偶然发现的主题的优秀读物:blogs.msdn.com/oldnewthing/archive/2009/02/25/9443404.aspx
-
@David:谢谢你的链接!你知道如何检查行尾是 LF 吗?维基百科告诉我们LF是U+000A。 Therms的建议可能是解决第二个问题的办法。