问题描述
我使用以下代码在python中创建了以下Flask应用程序(名为 simpleflask.py
):
I have created the following Flask application in python (named simpleflask.py
), using the code below:
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应用程序可以在我的个人计算机上正常运行.
The Flask app runs fine from my personal computer.
然后,我尝试使用 Flask文档中提供的说明创建.cgi应用程序,其中包含以下代码:
I then tried to create a .cgi application using the instructions provided in the Flask documentation, which features the following code:
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.
我不太确定这是什么意思.知道为什么我会收到此错误吗?为了能够创建我的.cgi应用程序,我必须进行哪些更改?任何建议都欢迎.
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? Any and all suggestions welcome.
推荐答案
您的cgi脚本必须首先编写标头.至少是内容类型":
Your cgi-script has to write firstly headers. At least 'content-type':
Content-Type: text/html;
,后跟空白行.然后浏览您的内容.
with empty line after it.And then goes your content.
这篇关于"500内部服务器错误";尝试从flask应用程序创建.cgi应用程序时收到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!