Web 服务器的 .cgi 问题

发布于 2024-07-14 06:36:21 字数 1711 浏览 5 评论 0原文

代码

#!/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.

你如何解决这个问题?

[编辑] 在第一个答案

  1. test.cgi 是可执行的(我运行 $ chmod +x test.cgi)
  2. 之后我使用 Apache。
  3. 我在 .bashrc 中有这个导出 PATH=${PATH}:~/bin
  4. Python 模块 MySQLdb 已安装。
  5. 该代码没有智能引号。

第二个答案之后的[编辑]

你收到这个错误是因为你 还没有安装MySQLdb模块 Python 需要与 MySQL 对话 数据库

我在系统中安装了 MySQLdb。 该模块可以工作,因为我可以导入它们。 但是,当我访问 www.[mysite].com/test.cgi 时,我仍然遇到相同的错误。

[编辑]

我不确定这些问题

connect() 参数是否正确? MySQL 是否在本地主机上运行 在默认端口?

我在我的服务器上运行 MySQL。 关于 connect() 参数的问题与这里相关吗?

SELECT 语句正确吗?

你的意思是我的SQL语句例如SELECT语句是否正确? 我还没有使用过任何 SQL 查询。 我这里需要他们吗?

The code

#!/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()

I saved it as test.cgi to my web server. I run it by www.mysite.com/test.cgi unsuccessfully

Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.

How can you solve the problem?

[edit] after the first answer

  1. test.cgi is executable (I run $ chmod +x test.cgi)
  2. I use Apache.
  3. I have this in .bashrc export PATH=${PATH}:~/bin
  4. Python module MySQLdb is installed.
  5. The code does not have smart quotes.

[edit] after the second answer

you're getting that error because you
haven't installed the MySQLdb module
that Python needs to talk to a MySQL
database

I installed MySQLdb to my system. The module works, since I can import them.
However, I still get the same error whet I go to the www.[mysite].com/test.cgi.

[edit]

I am not sure about the questions

Are the connect() parameters correct? Is MySQL running on localhost
at the default port?

I run MySQL on my server. Is the question about the connect() parameters relevant here?

Is the SELECT statement correct?

You mean whether I have my SQL statements such as SELECT statement correct?
I have not used any SQL queries yet.
Do I need them here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

暖风昔人 2024-07-21 06:36:21

许多问题都可能导致您看到的错误:

  1. test.cgi 在服务器上可执行 (chmod 755) 吗?
  2. 是放置 test.cgi 的目录,指定为 ScriptAlias 位置或具有 ExecCGI启用选项(或等效选项,如果您不使用 Apache)?
  3. python 是在系统 PATH 中还是在 Web 服务器启动环境的 PATH 中?
  4. 是否安装了 MySQLdb Python 库?
  5. connect() 参数是否正确? MySQL 是否在本地主机的默认端口上运行?
  6. SELECT 语句正确吗?

如果您确定找到了 python(使用最简单的脚本进行测试,或者通过登录到 Web 服务器(如果可以的话)并输入 which python),那么您可以获得很多信息通过将以下内容添加到脚本顶部的 shebang 下方,可以更好地调试输出:

import cgitb
cgitb.enable()

更多详细信息:http ://docs.python.org/library/cgitb.html

此外,如果您具有对 Web 服务器的 shell 访问权限,请尝试运行 python 并键入:

>>> import MySQLdb

如果命令返回且没有错误,则您将获得以下答案:上面#4。 如果打印错误,则需要将 MySQLdb 安装到 Web 服务器的 Python 安装中。

编辑:仔细观察你的问题的顶部,我发现代码是从 Django Book 开头的说明性示例中删除的。 因此,我可能会扩展上面的#5,以包含一个警告,即,当然,需要在可用于 Web 服务器的 MySQL 安装上设置必要的数据库、表、用户和权限。

Any number of issues can cause the error you are seeing:

  1. Is test.cgi executable (chmod 755) on the server?
  2. Is the directory in which you placed test.cgi designated as a ScriptAlias location or have the ExecCGI option enabled (or equivalent if you're not using Apache)?
  3. Is python in the system PATH or in the PATH in the Web server's startup environment?
  4. Is the MySQLdb Python library installed?
  5. Are the connect() parameters correct? Is MySQL running on localhost at the default port?
  6. Is the SELECT statement correct?

If you're sure that python is found (test using the simplest possible script or by logging into the Web server if you can and typing which python) then you can get much better debug output by adding the following to the top of your script just below the shebang:

import cgitb
cgitb.enable()

More details: http://docs.python.org/library/cgitb.html

Additionally, if you have shell access to the Web server, try running python and just typing:

>>> import MySQLdb

If the command returns with no error, you have your answer for #4 above. If an error is printed, you will need to get MySQLdb installed into the Web server's Python installation.

EDIT: Looking more closely at the top of your question, I see that the code was scraped from an illustrative example at the very beginning of the Django Book. As such, I might expand #5 above to include the caveat that, of course, the requisite database, tables, user, and permissions need to be set up on the MySQL installation available to the Web server.

勿忘心安 2024-07-21 06:36:21

我通过在必要时插入换行符并用 "' 替换智能引号对代码进行了一些整理。您对以下内容还有更多的运气吗?您可以运行只需在终端输入python test.cgi就可以了?

#!/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()

I've tidied up the code a bit by inserting linebreaks where necessary and replacing smart quotes with " and '. Do you have any more luck with the following? Can you run it from a terminal just by typing python test.cgi?

#!/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()
酷到爆炸 2024-07-21 06:36:21

http://dpaste.com/8866/ 中的错误发生是因为您使用了“大引号”而不是标准 ASCII 引号。

您需要将“和”替换为“。只需在文本编辑器中使用查找和替换即可。

The error in http://dpaste.com/8866/ is occurring because you are using "curly quotes" instead of standard ASCII quotation marks.

You'll want to replace the “ and ” with ". Just use find and replace in your text editor.

水晶透心 2024-07-21 06:36:21

确保使用正确的行结尾保存文件。 即 LF 仅在 UNIX 上,或 CR/LF 在 Windows 上。 我最近遇到了完全相同的问题......

Make sure you're saving the file with correct line endings. i.e. LF only on unix, or CR/LF for Windows. I just recently had the exact same problem...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文