2017-05-25 59 views
0

我見過很多其他答案並嘗試過,但他們沒有幫助。Apache:標題前腳本輸出結束

我的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 
[[email protected] cgi-bin]# pwd 
/var/www/mysite.com/cgi-bin 

虛擬主機的conf

[[email protected] 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> 
[[email protected] 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 

如果需要更多信息,請讓我知道。

謝謝。

回答

0

首先,我把它作爲web服務器以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> 
""") 
+0

我已經刪除了空行但它仍然不工作。請幫忙。 –

1

我懷疑你的問題可能是

print() 

在Python 2.7.12至少這個打印()而不是你所期望的

請張貼在一個空行確切的輸出腳本,因爲它是現在。

相關問題