10

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.

Paco
  • 4,520
  • 3
  • 29
  • 53
monostop
  • 569
  • 1
  • 6
  • 17
  • 1
    Do you run it under some http server ? Those environment vars should be set by the http server executing the cgi. – mkriheli Sep 06 '11 at 21:27
  • Thanks for the comment.I'm running apache for hosting. I didn't think I needed to configure the server just to run the commands locally. I will try again after I have made the configurations needed. – monostop Sep 07 '11 at 17:42
  • Did you able to fix this error ? – Shadkhan Jun 08 '17 at 06:39
  • This question is three years old, and was never flagged as answered. If you're looking for a pretty complete discussion of how to do it see the most recent post, by me, to [this related stackoverflow question](https://stackoverflow.com/questions/18259435/deploy-flask-application-on-11-shared-hosting-with-cgi). It should work for you. Certainly mkriheli has the right idea; monostop should not be trying to run the cgi file (let the server do that in response to an incoming HTTP request). – Mike O'Connor Jul 20 '14 at 07:57
  • Indeed, http://stackoverflow.com/a/24848407/2371522 worked for me. – One Feb 09 '15 at 23:41

2 Answers2

3

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.

Dinidiniz
  • 771
  • 9
  • 15
0

This is posted as an answer following the comments above for the sake of completeness.

As discussed above, cgi scripts should execute by some server. Here's the abstract from CGI 1.1 RFC:

The Common Gateway Interface (CGI) is a simple interface for running external programs, software or gateways under an information server in a platform-independent manner. Currently, the supported information servers are HTTP servers.

For the environment variables (which were missing and triggered the error) see sectuib 4.1 in the RFC.

mkriheli
  • 1,788
  • 10
  • 18