脚本index.py中的头格式不正确

2024-05-14 09:29:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在apache2(ubuntu 14.04)服务器上运行python代码。我已经遵循了这些步骤并得到了错误:

步骤1:

配置1:我在

/var/www/cgi-bin

配置2:我已经编辑了/etc/apache2/sites available/000-default.conf

Alias /cgi-bin /var/www/cgi-bin
<Directory "/var/www/cgi-bin">
Options Indexes FollowSymLinks ExecCGI
AddHandler cgi-script .cgi .py
Allow from all
</Directory>

<Directory /var/www/cgi-bin>
Options All
</Directory>

步骤2:

我的python脚本是:index.py

#!/usr/bin/env python
import cgi;
import cgitb;cgitb.enable()

print "Content-Type: text/plain\n"

print "<b>Hello python</b>"

步骤3:

当我使用chrome浏览器运行时: 网址:http://localhost/cgi-bin/index.py

步骤4:

我在错误日志中得到这个错误

malformed header from script 'index.py': Bad header: Hello Python

Tags: frompyimportindexbinvarwww错误
2条回答

尝试此脚本

#!/usr/bin/env python
import cgi;
import cgitb;cgitb.enable()

print "Content-Type: text/html"
print "" #use this double quote print statement to add a blank line in the script
print "<b>Hello python</b>"

标题和主html内容之间应该有一行空格。这就是为什么在脚本中启动html标记之前必须使用额外的print语句。

你应该用\r\n结束你的头,然后你必须打印出另一个\r\n来表示尸体来了。

(换句话说,它将您的身体解释为头,因为头从未终止)

相关问题 更多 >

    热门问题