运行python脚本作为CGI Apache服务器 [英] run python script as cgi apache server
问题描述
我试图让作为运行CGI Python脚本,使用Apache服务器。我的脚本看起来像这样:
I am trying to make a python script run as cgi, using an Apache server. My script looks something like this:
#!/usr/bin/python
import cgi
if __name__ == "__main__":
print("Content-type: text/html")
print("<HTML>")
print("<HEAD>")
我已经做在httpd.conf进行必要的设置(在我看来):
I have done the necessary configurations in httpd.conf(in my opinion):
<Directory "/opt/lampp/htdocs/xampp/python">
Options +ExecCGI
AddHandler cgi-script .cgi .py
Order allow,deny
Allow from all
</Directory>
我已经设置了执行权限用chmod脚本
I have set the execution permission for the script with chmod
然而,当我试图通过本地主机访问脚本我得到一个错误500:头前脚本输出的结束:script.py
可能是什么问题?所以我觉得CLRF VS LF的问题不站脚本是在Unix的创建般的环境。非常感谢。
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.
推荐答案
我觉得你失踪后
print("Content-type: text/html")
CGI脚本的输出应该包括两个部分,一个空行隔开。第一部分包含了一些头,告诉客户什么样的数据是
以下内容。
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.
第二部分一般是HTML,它允许客户端软件,以显示与标题,在线图像很好地格式化文本,等等。
The second section is usually HTML, which allows the client software to display nicely formatted text with header, in-line images, etc.
这可能看起来像
#!/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!
"""
有关更多详情,请访问蟒蛇-CGI
For more details visit python-cgi
有关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!
"""
)
这篇关于运行python脚本作为CGI Apache服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!