将 python 脚本作为 cgi apache 服务器运行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15878010/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
run python script as cgi apache server
提问by biggdman
I am trying to make a python script run as cgi, using an Apache server. My script looks something like this:
我正在尝试使用 Apache 服务器将 python 脚本作为 cgi 运行。我的脚本看起来像这样:
#!/usr/bin/python
import cgi
if __name__ == "__main__":
print("Content-type: text/html")
print("<HTML>")
print("<HEAD>")
I have done the necessary configurations in httpd.conf(in my opinion):
我已经在 httpd.conf 中做了必要的配置(在我看来):
<Directory "/opt/lampp/htdocs/xampp/python">
Options +ExecCGI
AddHandler cgi-script .cgi .py
Order allow,deny
Allow from all
</Directory>
I have set the execution permission for the script with chmod
我已经用 chmod 设置了脚本的执行权限
However, when I try to access the script via localhost i get an Error 500:End of script output before headers:script.py What could be the problem? The script is created in an Unix like environment so I think the problem of clrf vs lf doesn't stand. Thanks a lot.
但是,当我尝试通过 localhost 访问脚本时,我收到错误 500:End of script output before headers:script.py 可能是什么问题?该脚本是在类 Unix 环境中创建的,所以我认为 clrf 与 lf 的问题不成立。非常感谢。
采纳答案by Ahsan Habib
I think you are missing a print statement after
我认为您在之后缺少打印语句
print("Content-type: text/html")
The output of a CGI script should consist of two sections, separated by a blank line. The first section contains a number of headers, telling the client what kind of data is following.
CGI 脚本的输出应由两部分组成,由空行分隔。第一部分包含许多标题,告诉客户端下面是什么类型的数据。
The second section is usually HTML, which allows the client software to display nicely formatted text with header, in-line images, etc.
第二部分通常是 HTML,它允许客户端软件显示带有标题、内嵌图像等的格式良好的文本。
It may look like
它可能看起来像
#!/usr/bin/env python
print "Content-Type: text/html"
print
print """
<TITLE>CGI script ! Python</TITLE>
<H1>This is my first CGI script</H1>
Hello, world!
"""
For more details visit python-cgi
有关更多详细信息,请访问python-cgi
For python3
对于python3
#!/usr/bin/env python3
print("Content-Type: text/html")
print()
print ("""
<TITLE>CGI script ! Python</TITLE>
<H1>This is my first CGI script</H1>
Hello, world!
"""
)

