tags:

views:

956

answers:

2

Hi everyone,

There's not much documentation surrounding the python-fastcgi C library, so I'm wondering if someone could provide a simple example on how to make a simple FastCGI server with it. A "Hello World" example would be great.

+3  A: 

Edit: I misread the question. Ooops.

Jon's Python modules is a collection of useful modules and includes a great FastCGI module: http://jonpy.sourceforge.net/fcgi.html

Here's the example from the page:

import jon.cgi as cgi 
import jon.fcgi as fcgi

class Handler(cgi.Handler):
  def process(self, req):
    req.set_header("Content-Type", "text/plain")
    req.write("Hello, world!\n")

fcgi.Server({fcgi.FCGI_RESPONDER: Handler}).run()
a paid nerd
+1  A: 

I would recommend using a fastcgi WSGI wrapper such as this one, so you aren't tied in to the fastcgi approach from the start.

And then a simple test.fgi file like such:

#!/usr/bin/env python

from fcgi import WSGIServer

def app(env, start):

    start('200 OK', [('Content-Type', 'text/plain')])
    yield 'Hello, World!\n'
    yield '\n'

    yield 'Your environment is:\n'
    for k, v in sorted(env.items()):
        yield '\t%s: %r\n' % (k, v)

WSGIServer(app).run()
Mike Boers