Use Python with FastGCI
Posted by ASO Admin on 11 February 2020 08:37 AM
|
|
The first part of these instructions are similar to Django on FastCGI. First, log into SSH (shell) and then: cd ~/www mkdir project cd project wget http://svn.saddi.com/py-lib/trunk/fcgi.py chmod 755 fcgi.py
RewriteEngine On RewriteBase /project RewriteCond %{REQUEST_URI} !^/project/dispatch.fcgi [NC] RewriteCond %{REQUEST_URI} !^/project/static [NC] RewriteCond %{REQUEST_URI} !^/project/more_static [NC] RewriteCond %{REQUEST_URI} !\.(css|png|jpg|gif)$ [NC] RewriteRule ^(.*)$ dispatch.fcgi/$1 [QSA,NC]
Now, create project/dispatch.fcgi with Unix line endings. You will receive puzzling FastCGI errors if you use any other line endings, especially if you are on Windows like me and don't like spending hours on end diagnosing puzzling FastCGI errors. #!/usr/bin/env python # This must be in Unix line endings import sys from fcgi import WSGIServer import application import cgi class Request(object): pass def app(e, start_response): req = Request() # POST data req.form = cgi.FieldStorage(fp = e['wsgi.input'], environ = e) # GET data req.params = cgi.parse_qs(e['QUERY_STRING']) start_response('200 OK', [('Content-type', 'text/html')]) return [application.start(req)] WSGIServer(app).run()
Before accessing it and possibly forcing FastCGI to generate a broken cache (see below), type python dispatch.fcgi in your shell. You should see some output although the environ variables aren't set properly from the shell. If you see Python errors, though, this is a quick way to partially debug your application without having to go through FastCGI just yet. Now navigate to example.com/project/ or whereever and you should see output. Remember: example.com/project/jobs maps to example.com/project/dispatch.fcgi/jobs due to .htaccess. TipsIf you want to update your application, simply run touch dispatch.fcgi in your shell. If your dispatch.fcgi is somehow broken, FastCGI may add an error stating that it couldn't get the file to run after three requests and has backed off the reload interval to some ungodly time. (You can see this in the error log from your control panel. Unfortunately, FastCGI is then unable to detect changes to your file so you need to wait a while before you start fixing the problem. (I can't seem to find a workaround besides renaming dispatch.fcgi, but that gets tedious.) By the way, that fcgi.py will print to output very useful exceptions and tracebacks if your module throws an uncaught exception using the cgitb module. This is, however, undesirable for production web sites since it does expose lines of your code to your users. To remedy this, you can monkeypatch the function or just manually edit the Server.error method in fcgi.py. (Grep for "cgitb.") To redirect, start_response('302 Found', [('Location', url)]). | |
|