Path routing in Flask
Asked Answered
C

2

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.

Candicecandid answered 2/4, 2012 at 18:53 Comment(9)
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.Whaley
Does your host only support CGI or do they support mod_python or mod_wsgi? Those are much better options.Whaley
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. ThanksCandicecandid
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 or if you are looking for cheap Python hosting with real Python support, try heroku.com which you can use for free. docs.python-guide.org is a good resource to get started with Python and Python hosting.Whaley
Thanks, @Cixate. I will love to have better performance. To use WSGI, don't I need admin right to config the <VirtualHost *> first? It will be nice if I can pull this off from my PHP-friendly hosting plan.Candicecandid
@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?Xerophagy
@PauloScardine: Sure. It is 1and1.com. At this time, I have a virtualenv setup. That's where Flask was installed. ThanksCandicecandid
@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.Xerophagy
@PauloScardine: I got that feeling already. Thanks.Candicecandid
K
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]
Klemens answered 6/4, 2012 at 12:47 Comment(0)
R
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

Refund answered 30/5, 2016 at 10:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.