1

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.

loic17
  • 727
  • 6
  • 14
  • I don't think .cgi is the way to go. Have a look at the documentation: http://flask.pocoo.org/docs/0.10/deploying/. I would recommend wsgi : http://flask.pocoo.org/docs/0.10/deploying/wsgi-standalone/ – Paco Dec 19 '14 at 10:59
  • @Paco Why would you recommend wsgi over .cgi? – loic17 Dec 19 '14 at 19:13
  • It's not about speed (as the question would suggest, but read the accepted answer) http://stackoverflow.com/questions/1747266/is-there-a-speed-difference-between-wsgi-and-fcgi – Paco Dec 20 '14 at 19:04

1 Answers1

1

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.

Vladyslav Savchenko
  • 1,282
  • 13
  • 10