【发布时间】:2012-08-04 23:31:12
【问题描述】:
出于某种原因,我必须在我的 python cgi 脚本中使用分号。此外,它无法识别 if 语句的常用语法。
例如,这是有效的:
#! /usr/bin/python
print "Content-type: text/html\n\n";
print "<html>Hello World!</html>";
但这不起作用:
#! /usr/bin/python
print "Content-type: text/html\n\n"
print "<html>Hello World!</html>"
这不起作用:
#! /usr/bin/python
print "Content-type: text/html\n\n";
if True:
print "<html>Hello World!</html>";
我从最后两个示例中得到 500 个内部服务器错误。我的 apache 错误日志说文件中存在语法问题。我在 OSX 上在 Apache2 上运行它。非常感谢任何帮助!
编辑:所以显然这不是作为 python 脚本处理的。我想我可能已经找到了问题,但我不确定如何解决它。在我的 httpd.conf 中,我在末尾附加了以下几行,我认为它们是问题所在:
<Location /cgi-bin>
SetHandler perl-script
PerlResponseHandler ModPerl::Registry
Options ExecCGI
PerlSendHeader On
Order allow,deny
Allow from all
</Location>
我有这些行是为了让我的 cgi-bin 中的一些其他 cgi 脚本与 mod_perl 一起运行。如果这需要更改,它应该是什么样子以便 perl cgi 文件仍将在 mod_perl 下运行,但 python cgi 文件将作为 python 运行?为了清楚起见或以防万一,我的 cgi-bin 中的 perl 文件具有 .cgi 文件扩展名,但我尝试使用的 python cgi 文件具有 .py 文件扩展名。
【问题讨论】:
-
尝试删除
#!之后的空格。听起来您的脚本正在由 Python 解释器以外的其他东西运行。 -
试过了,没用。在终端中,“whereis python”返回 /usr/bin/python 所以我认为我在正确的地方
-
那么,您在错误日志文件中得到的错误信息是什么?
-
你确定python实际上位于
/usr/bin/python吗?which python说什么? -
mod_perl 仅用于为 CGI 运行 Perl 脚本。如果要将 Python 脚本作为 CGI 运行,则必须安装 mod_python、mod_wsgi、vanilla CGI 或其他一些可以运行 Python 脚本的 Apache mod。您的 httpd.conf 告诉 Apache:在 CGI 脚本中处理此目录中的所有文件,这些文件应由 mod_perl 处理。如果您安装了 mod_wsgi 或其他其中之一,您应该能够运行 Perl 或 Python 脚本,但您将修改您的 httpd.conf 以便 Apache 可以确定哪个是合适的 mod 使用。
标签: python apache cgi syntax-error