Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python-fastcgi extension

Tags:

python

fastcgi

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.

like image 489
Zoip Avatar asked Mar 02 '26 04:03

Zoip


2 Answers

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()
like image 153
a paid nerd Avatar answered Mar 03 '26 18:03

a paid nerd


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()
like image 20
Mike Boers Avatar answered Mar 03 '26 17:03

Mike Boers



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!