将 python 脚本作为 cgi apache 服务器运行 [英] run python script as cgi apache server

查看:33
本文介绍了将 python 脚本作为 cgi apache 服务器运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Apache 服务器将 python 脚本作为 cgi 运行.我的脚本看起来像这样:

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:End of script output before headers:script.py可能是什么问题呢?该脚本是在类 Unix 环境中创建的,所以我认为 clrf 与 lf 的问题不成立.非常感谢.

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 脚本的输出应由两部分组成,由空行分隔.第一部分包含多个headers,告诉客户端是什么类型的数据跟随.

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!
"""

欲了解更多详情,请访问 python-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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆