【发布时间】:2015-01-28 19:26:44
【问题描述】:
我正在尝试在我的服务器上使用 python CGI 脚本。下面显示的python和perl脚本都在同一个目录下,权限为755。
如果我使用此代码尝试 python hello world,并将浏览器指向它:
#!/usr/bin/env python
import cgi
import cgitb
cgitb.enable()
print "Content-type:text/html\r\n\r\n"
print "Hello, World!"
它失败了:Error 500: Premature end of script headers: cgiwrap
我认为我有一个cgiwrap 权限问题,因为我的服务器是 apache,我读到 perl 中的 -w 选项可以绕过这类问题。
有趣的是,我尝试使用主机提供程序帮助页面上提供的 perl Hello World(按原样运行时失败):
#!/usr/bin/perl
print "Content-type: text/html\r\n\r\n";
print "Hello World";
它失败了:CGIWrap Error: Script Execution Failed, Error Message: No such file or directory
如果我尝试执行(仅更改 shebang):
#!/usr/bin/perl -wT
print "Content-type: text/html\r\n\r\n";
print "Hello World";
它运行...
此外,终端中的命令python index.py 按预期运行,而./index.py 失败并显示No such file or directory
which python 命令输出:/usr/bin/python
【问题讨论】:
-
没有cgi经验,不知道你是否需要在渲染任何实际内容之前显式打印
<html>和<body>标签。 -
在“Content-type”之后打印空行时,您可能使用了错误类型的换行符。试试
print "Content-type: text/html\r\n\r\n" -
@BhargavRao 我试过了,是的。也许我做错了?我编辑以尝试您的所有建议。顺便说一句,感谢大家的帮助,你们真的很好!