10

I want to run a Python CGI on a shared hosting environment. I followed Flask's example and came up with a tiny application as below:

from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello World!"

@app.route("/pi")
def pi():
    return "3.1416"

if __name__ == "__main__":
    app.run()

My .htaccess contains:

Options +ExecCGI 
AddHandler cgi-script .cgi .py .rb
DirectoryIndex index.cgi index.htm

And my index.cgi is

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

CGIHandler().run(app)

It successfully maps the path / to index(), however it fails to map the path /pi to pi(), instead returning a 404 error. I guess I miss something obvious. Thanks for the help.

sdc
  • 101
  • 4
  • Is this with Apache? I think adding an Apache tag will likely get you more answers since this seems to be more of a web server configuration problem. – six8 Apr 03 '12 at 00:46
  • Does your host only support CGI or do they support mod_python or mod_wsgi? Those are much better options. – six8 Apr 03 '12 at 00:53
  • Cixate, you are right this is with Apache. I am new to python web-frameworks. The cgi setting seems quicker than mod-wsgi to setup. That's why I choose cgi. Another thing, I suspected it may need url-rewrite. But because I did not see any url-rewrite mentioned in the document, so I assume there is no need. Thanks – sdc Apr 03 '12 at 02:37
  • CGI may be quicker to setup but is much worse at performing than WSGI as the python app is in effect started and stopped each request. You can set up WSGI [like this](http://flask.pocoo.org/docs/deploying/mod_wsgi/) or if you are looking for cheap Python hosting with real Python support, try http://www.heroku.com/ which you can use for free. http://docs.python-guide.org/ is a good resource to get started with Python and Python hosting. – six8 Apr 03 '12 at 03:43
  • Thanks, @Cixate. I will love to have better performance. To use WSGI, don't I need admin right to config the `` first? It will be nice if I can pull this off from my PHP-friendly hosting plan. – sdc Apr 03 '12 at 05:46
  • @sdc: many ot the cheap PHP-friendly hosting out there have some helpers in place to deploy Django apps. Would you mind naming your service provider? – Paulo Scardine Apr 03 '12 at 05:50
  • @PauloScardine: Sure. It is 1and1.com. At this time, I have a virtualenv setup. That's where Flask was installed. Thanks – sdc Apr 03 '12 at 13:41
  • @sdc: unfortunately `1and1.com` lacks proper support for Python/Django. The performance penalty and amount of time you will spend fighting the environment is not worth. – Paulo Scardine Apr 03 '12 at 21:29
  • @PauloScardine: I got that feeling already. Thanks. – sdc Apr 04 '12 at 03:09

2 Answers2

2

Comments about cgi vs. wsgi are valid, but if you really want to go with cgi setup, you need some rewriting rules in order to catch URLs other than "/" with the index.cgi. With your setup you basically say that index file is index.cgi, but in case there is something else in the path index.cgi won't get executed. This is why you get 404 Not Found for /pi request.

You can access the pi() function by requesting url /index.cgi/pi and it will successfully render you 3.1416, but obviously that is not a very nice URL. That gives a hint about what needs to be configured for rewriting though: rewrite all requests with / to /index.cgi/. This gives very simple rewrite rules together with your original configuration:

Options +ExecCGI
AddHandler cgi-script .cgi .py .rb
DirectoryIndex index.cgi index.htm

RewriteEngine On
RewriteRule ^index.cgi/(.*)$ - [S=1]
RewriteRule ^(.*)$ index.cgi/$1 [QSA,L]
Jari
  • 2,152
  • 13
  • 20
0

Not sure if the above solution solved the query.

I think the .htaccess file needs to be this:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^(.*)$ /path/to/the/application.cgi/$1 [L]

It will work perfectly with this. I have tested with my code.

Reference - here

Ejaz
  • 1,504
  • 3
  • 25
  • 51