Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deploying a Flask application with CGI [duplicate]

Tags:

python

flask

cgi

I have written a small application using the Flask framework. I try to host this using cgi. Following the documentation I created a .cgi file with the following content:

#!/usr/bin/python
from wsgiref.handlers import CGIHandler
from yourapplication import app

CGIHandler().run(app)

Running the file results in following error:

...

File "/usr/lib/pymodules/python2.7/werkzeug/routing.py", line 1075, in bind_to_environ wsgi_server_name = environ.get('HTTP_HOST', environ['SERVER_NAME'])
KeyError: 'SERVER_NAME'
Status: 500 Internal Server Error
Content-Type: text/plain
Content-Length: 59

In my application I have set:

app.config['SERVER_NAME'] = 'localhost:5000'

When I run the application with the Flask development server it works perfectly well. As you can tell I'm very new to this stuff and I have search for others with similar errors but with no luck. All help is appreciated.

like image 640
monostop Avatar asked Sep 06 '11 18:09

monostop


1 Answers

I will try to show what I've done and it is working in Godaddy sharing host account:

In the cgi-bin folder in MYSITE folder, I added the following cgi file:

#!/home/USERNAME/.local/bin/python3
from wsgiref.handlers import CGIHandler

from sys import path
path.insert(0, '/home/USERNAME/public_html/MYSITE/')
from __init__ import app

class ProxyFix(object):
   def __init__(self, app):
       self.app = app

   def __call__(self, environ, start_response):
       environ['SERVER_NAME'] = ""
       environ['SERVER_PORT'] = "80"
       environ['REQUEST_METHOD'] = "GET"
       environ['SCRIPT_NAME'] = ""
       environ['QUERY_STRING'] = ""
       environ['SERVER_PROTOCOL'] = "HTTP/1.1"
       return self.app(environ, start_response)

if __name__ == '__main__':
    app.wsgi_app = ProxyFix(app.wsgi_app)
    CGIHandler().run(app)

As you can see the init file in the MYSITE folder have the flask app.

The most important thing is to set the permissions right. I setted 755 to this folder permission AS WELL AS to "/home/USERNAME/.local/bin/python3" folder!! Remember that the system needs this permission to open flask.

To open the cgi I have the following .htaccess file in MYSITE folder:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^(.*)$ /home/USERNAME/public_html/MYSITE/cgi-bin/application.cgi/$1 [L]

So it will render the cgi file when someone enters your page.

like image 94
Dinidiniz Avatar answered Oct 16 '22 10:10

Dinidiniz