Python脚本头错误的过早结束

标签 python apache ubuntu cgi wsgi

我正在运行一个新的 python 脚本,经过多次试验,我已经让它运行了(有点)。这是文件:

#!/usr/bin/python
import cgi
import cgitb

print "Content-type: text/html"
print

print "this is working"

它在 SSH 中运行良好,但在浏览器中我收到 500 错误。查阅错误日志,我得到“脚本头过早结束”。我正在使用 mod_wsgi 运行 Ubuntu,并且我相信我已经正确设置了 apache2.conf,站点可用/默认设置正确,并且我拥有适当的权限并且没有正确设置。而且,就像我说的,python 在 SSH 中运行得很好——但我需要它作为 Web 应用程序运行。

任何人的任何想法吗?我已经为此工作了两天,但没有任何工作。

最佳答案

如果这是通过 mod_wsgi 编写的 wsgi 脚本,它应该如下所示:

def application (env, r):
    body = 'this is working'
    status = '200 OK'
    response_headers = [ ('Content-Type', 'text/html'), ('Content-Length', str (len (body) ) ) ]
    r (status, response_headers)
    return [body]

关于Python脚本头错误的过早结束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13888217/

相关文章:

python - 如何仅可视化高相关性以更好地表示巨大的相关矩阵?

php - 允许在 Ubuntu Server 中的 apache2 上创建文件夹

python - 在这种情况下如何重命名多个文件.jpg?

python - 根据最后一次出现的分隔符将字符串拆分为 2

angular - 使用 Angular 配置 Tomcat 8

javascript - 同一域上的 iframe 的权限被拒绝 - Apache vhost cfg 问题?

git - 将 git 存储库移动到 nfs 会导致权限错误

node.js - 在 Ubuntu 16.10 上安装 nodejs 的问题

ubuntu - WPEWebKit 无法在 Ubuntu 16.04 上启动

python - 如何使用 pymysql 作为驱动程序配置 Django?