Python Apache 本地/内部服务器错误?

0 投票
1 回答
908 浏览
提问于 2025-04-16 00:11

我正在创建一个HTML表单,并用Python来编写代码。我安装了Apache 2.2版本和Python 2.6版本。以下是我的代码:

#!C:\Python26\Python.exe -u

#import cgi modules
import cgi

#create instances of field storage
form = cgi.FieldStorage()

#get data from the fields
first_name = form.getvalue('first_name')
last_name  = form.getvalue('last_name')


print "Content-type:text/html\r\n\r\n"
print "<html>"
print "<head>"
print "<title>Hello - Second CGI Program</title>"
print "</head>"
print "<body>"
print "<h2>Hello %s %s</h2>" % (first_name, last_name)
print "</body>"
print "</html>"

我写的代码出现了内部错误。但是,当我删除以下几行时,代码就能正常运行。

#import cgi modules
    import cgi

    #create instances of field storage
    form = cgi.FieldStorage()

    #get data from the fields
    first_name = form.getvalue('first_name')
    last_name  = form.getvalue('last_name')

我编辑了httpd配置文件,并添加了以下几行:

AddHandler cgi-script .cgi .py .pl

ScriptAlias /cgi-bin/ "C:/Program Files (x86)/Apache Software Foundation/Apache2.2/cgi-bin/"

为什么添加这些行后会显示内部错误信息呢?而且,我对用Python构建HTML表单还是很陌生。如果能推荐一些关于在Windows上使用Python制作HTML表单的教程,那就太好了。谢谢。

附:包含脚本的文件位于Apache的cgi-bin文件夹中。

1 个回答

0

如果你把那些代码删掉了,程序还能跑,那肯定是哪里出了问题。print "<h2>Hello %s %s</h2>" % (first_name, last_name) 这行代码在你的HTML里应该会报错,因为这两个变量(first_name和last_name)还没有定义。

请把你错误日志里的相关内容发出来,这样我们可以看看是什么导致了内部服务器错误。

撰写回答