[英]“500 Internal Server Error” received when trying to create a .cgi application from a flask application
I have created the following Flask application in python (named simpleflask.py
), using the code below: 我使用以下代码在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()
The Flask app runs fine from my personal computer. Flask应用程序可以在我的个人计算机上正常运行。
I then tried to create a .cgi application using the instructions provided in the Flask documentation , which features the following code: 然后,我尝试使用Flask文档中提供的说明来创建.cgi应用程序,该文档具有以下代码:
from wsgiref.handlers import CGIHandler
from simpleflask import app
CGIHandler().run(app)
However, it does not work as expected. 但是,它不能按预期方式工作。 I keep receiving the following error message: 我不断收到以下错误消息:
KeyError: 'SERVER_NAME'
Status: 500 Internal Server Error
Content-Type: text/plain
Content-Length: 59
A server error occurred. Please contact the administrator.
I am not quite sure what this is supposed to mean. 我不太确定这是什么意思。 Any idea why I am receiving this error? 知道为什么我会收到此错误吗? What changes would I have to make to be able to create my .cgi application? 为了能够创建我的.cgi应用程序,我必须进行哪些更改? Any and all suggestions welcome. 任何和所有建议,欢迎。
Your cgi-script has to write firstly headers. 您的cgi-script必须先编写标头。 At least 'content-type': 至少是“内容类型”:
Content-Type: text/html;
with empty line after it. 之后是空行。 And then goes your content. 然后去你的内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.