.cgi Web服务器问题 [英] .cgi problem with web server

查看:153
本文介绍了.cgi Web服务器问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码

#!/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运行失败

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.

如何解决这个问题?

第一个答案之后


  1. test.cgi是可执行文件(我运行$ chmod + x test .cgi)

  2. 我使用Apache。

  3. 我在.bashrc导出中有此路径PATH = $ {PATH}:〜/ bin

  4. 已安装Python模块MySQLdb。

  5. 该代码没有智能引号。

  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] 答案


您会收到该错误,因为您
尚未安装Python需要安装的MySQLdb模块
与MySQL
数据库交谈

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

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

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.

我不确定这些问题


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

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

我在服务器上运行MySQL。

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


SELECT语句正确吗?

Is the SELECT statement correct?

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

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?

推荐答案

任何数量的问题都可能导致您看到错误:

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


  1. 服务器上的 test.cgi 可执行文件(chmod 755)是吗?

  2. 是放置 test.cgi 的目录,指定为 ScriptAlias 位置或具有 ExecCGI 选项(如果您未使用Apache,则是否等效)?

  3. python 是在系统 PATH 中还是在Web服务器的启动环境中的 PATH 中?

  4. 是否已安装 MySQLdb Python库?

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

  6. SELECT 语句正确吗?

  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?

如果您确定找到了 python (请使用最简单的脚本或通过登录Web服务器进行测试,如果您可以输入哪个python ),然后通过在脚本顶部的shebang下方添加以下内容来获得更好的调试输出:

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()

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

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

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

>>> import MySQLdb

如果命令没有错误返回,则对上面的#4有答案。如果打印出错误,则需要将MySQLdb安装到Web服务器的Python安装中。

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.

编辑:在顶部仔细查看关于您的问题,我发现该代码是从Django书开始时的一个示例中删除的。因此,我可能在上面的#5中进行扩展,以包含一个警告,当然,必须在可用于Web服务器的MySQL安装上设置必需的数据库,表,用户和权限。

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.

这篇关于.cgi Web服务器问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆