Apache:标头之前的脚本输出结束

时间:2017-05-25 07:47:45

标签: python apache cgi

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

我的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

如果需要更多信息,请与我们联系。

谢谢。

2 个答案:

答案 0 :(得分:1)

我怀疑你的问题可能是

print()

在Python 2.7.12中,至少打印()而不是你想象的空行

请立即发布脚本的确切输出。

答案 1 :(得分:0)

首先,我认为你是web-server以root身份运行,否则你的执行文件权限是错误的。

其次,如果从命令行运行该文件,则输出:

Content-Type: text/html


    <TITLE>CGI script ! Python</TITLE>
    <H1>This is my first CGI script</H1>

注意有两个空行而不是一行,这很糟糕,因为应该只有一行看https://stackoverflow.com/a/4913653/7220776

但是如果你稍微更改你的代码以删除其他空行就可以了。

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