【问题标题】:Apache: End of script output before headersApache:标头之前的脚本输出结束
【发布时间】:2017-05-25 07:47:44
【问题描述】:

我看过许多其他答案并尝试过,但它们没有帮助。

我的 Python 代码

#!/usr/bin/env python3
print("Content-Type: text/html")
print()
print ("""
    <TITLE>CGI script ! Python</TITLE>
    <H1>This is my first CGI script</H1>
 """)

它的位置和权限

-rwxr-xr-x. 1 root   root   161 May 24 02:42 mypython.py
[root@server cgi-bin]# pwd
/var/www/mysite.com/cgi-bin

虚拟主机配置

[root@server cgi-bin]# cat /etc/httpd/sites-available/mysite.com.conf
<VirtualHost *:80>
    ServerName www.mysite.com
    DocumentRoot /var/www/mysite.com/public_html
    ServerAlias mysite.com
    ScriptAlias /cgi-bin/ "/var/www/mysite.com/cgi-bin/"
    <Directory /var/www/mysite.com>
        Options Indexes FollowSymLinks Includes ExecCGI
        AddHandler cgi-script .cgi .py
        AllowOverride None
        Require all granted
    </Directory>
    ErrorLog /var/log/httpd/mysite.com/error.log
    CustomLog /var/log/httpd/mysite.com/requests.log combined
</VirtualHost>
[root@server cgi-bin]#

在尝试访问时,我遇到了错误

[Wed May 24 02:42:53.958318 2017] [cgid:error] [pid 7943] [client 192.168.56.1:52390] End of script output before headers: mypython.py
[Wed May 24 02:42:54.661338 2017] [cgid:error] [pid 7939] [client 192.168.56.1:52391] End of script output before headers: mypython.py
[Wed May 24 02:42:59.383215 2017] [cgid:error] [pid 7940] [client 192.168.56.1:52392] End of script output before headers: mypython.py

如果需要更多信息,请告诉我。

谢谢。

【问题讨论】:

    标签: python apache cgi


    【解决方案1】:

    我怀疑你的问题可能是

    print()
    

    在 Python 2.7.12 中,至少这会打印 () 而不是您可能期望的空行

    请按原样发布脚本的确切输出。

    【讨论】:

      【解决方案2】:

      首先我认为你的网络服务器是以root身份运行的,否则你的执行文件权限是错误的。

      其次,如果你从它输出的命令行运行文件:

      Content-Type: text/html
      
      
          <TITLE>CGI script ! Python</TITLE>
          <H1>This is my first CGI script</H1>
      

      注意有两个空行不是一个,这很糟糕,因为应该只有一个见https://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html

      但是,如果您稍微更改代码以删除另一个空行,它就可以工作。

      print ("""<TITLE>CGI script ! Python</TITLE>
          <H1>This is my first CGI script</H1>
       """)
      

      【讨论】:

      • 我已经删除了空行,但它仍然无法正常工作。请帮忙。
      猜你喜欢
      • 1970-01-01
      • 2013-12-01
      • 2018-05-30
      • 2016-03-24
      • 1970-01-01
      • 2020-10-26
      • 1970-01-01
      • 2014-04-13
      • 2014-05-03
      相关资源
      最近更新 更多