简体   繁体   中英

“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:

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.

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM