【问题标题】:"500 Internal Server Error" received when trying to create a .cgi application from a flask application尝试从烧瓶应用程序创建 .cgi 应用程序时收到“500 内部服务器错误”
【发布时间】:2014-12-19 10:19:43
【问题描述】:

我使用以下代码在 python 中创建了以下 Flask 应用程序(命名为 simpleflask.py):

from flask import Flask
import random

app = Flask(__name__)
app.secret_key = 'This is really unique and secret'

@app.route('/')
def index():
    a = random.randrange(2)
    if a == 1:
        return "<p>the random number selector returned 1</p>"
    else:
        return "<p>the random number selector returned 0</p>"

if __name__ == "__main__":
    app.run()

Flask 应用在我的个人电脑上运行良好。

然后我尝试使用Flask documentation 中提供的说明创建一个 .cgi 应用程序,其中包含以下代码:

from wsgiref.handlers import CGIHandler
from simpleflask import app

CGIHandler().run(app)

但是,它没有按预期工作。我不断收到以下错误消息:

KeyError: 'SERVER_NAME'
Status: 500 Internal Server Error
Content-Type: text/plain
Content-Length: 59

A server error occurred.  Please contact the administrator.

我不太确定这应该是什么意思。知道为什么我会收到此错误吗?我必须进行哪些更改才能创建我的 .cgi 应用程序?欢迎提出任何建议。

【问题讨论】:

标签: python flask cgi


【解决方案1】:

您的 cgi 脚本必须首先编写标题。至少“内容类型”:

Content-Type: text/html;

后面有空行。 然后是你的内容。

【讨论】:

  • 第一次打印前插入:print "Content-Type: text/html\r\n\r\n"
猜你喜欢
  • 2022-01-12
  • 1970-01-01
  • 2020-06-25
  • 1970-01-01
  • 2020-02-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-13
相关资源
最近更新 更多