2009-05-17 56 views
3

關於python-fastcgi C庫沒有太多的文檔,所以我想知道是否有人可以提供一個簡單的例子來說明如何用它製作一個簡單的FastCGI服務器。 「Hello World」例子會很棒。python-fastcgi擴展

回答

4

編輯:我誤解了這個問題。糟糕!

Jon's Python modules是有用的模塊的集合,包括一個偉大的FastCGI模塊:http://jonpy.sourceforge.net/fcgi.html

下面是從頁的例子:

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() 
3

我會建議使用FastCGI的WSGI包裝類如this one,使你從一開始就沒有被綁定到fastcgi方法。

然後簡單test.fgi文件中像這樣的:

#!/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()