If Not CGI...

John Nagle nagle at animats.com
Sun Jun 20 17:28:51 EDT 2010


On 6/19/2010 11:18 AM, Stephen Hansen wrote:
> FastCGI is a different kind of approach to the problem; it launches
> Python alongside Apache, and that Python stays alive forever. It just
> redirects requests to said process when they come in. I know very little
> about this model, but believe its meant to sort of mimic a CGI
> environment so you can sort of migrate to it easier, but I'm not
> entirely sure. So I can't comment on this directly, but
> http://docs.python.org/howto/webservers.html seems interesting (Though
> it speaks of ALL of these options, so is a good read anyways)
....
> But the thing is: a WSGI web application looks and is shaped nothing
> like a CGI application. They're awesome. Btu different. Writing a WSGI
> app from scratch without a framework is possible, but it seems like a
> terribly painful thing to go about doing.

   It's not that difficult.  WSGI works as an interface to FCGI in
Apache.  If you set up programs that way, they'll run either as
CGI programs (useful for debug) or WSGI programs.

   WSGI/FCGI programs look a lot like CGI programs.  The main
difference is that your transaction program is called as a subroutine,
and has to be reusable.  Avoid global state and that's easy.

   When you're called, you get various parameters, and at the
end, you return the HTTP content you want sent over the wire.

   WSGI/FCGI scales well.  The FCGI server will fire off extra
copies of the application as the load goes up, and will ask
unneeded copies to exit when the load goes down.  If an FCGI
program crashes, the server just restarts a fresh copy.
So it's a robust mechanism.

   Below is "Hello World" for FCGI/WSGI, without a "framework".

   The biggest headache is getting the "mod_fcgi" module into Apache
(or running "lighthttpd"), getting it configured properly,
and getting it to find your FCGI program.  (Note: if you put
an FCGI program in a directory that Apache considers valid
for CGI, Apache will run the program with CGI, not FCGI.
This works, but at CGI speeds, with a program reload every time.)

A FCGI/WSGI module for Python is here:

http://svn.saddi.com/py-lib/trunk/fcgi.py

					John Nagle

#!/usr/local/bin/python2.6
import fcgi

#
#	"Hello World" for FCGI/WSGI
#
def simpleApp(environ, start_response):
	status = '200 OK'
	headers = [('Content-type','text/plain')]
	start_response(status, headers)
	return ['Hello world from simple_application!\n']

#
#	Main FCGI program
#
fcgi.WSGIServer(simpleApp).run()




More information about the Python-list mailing list