python-fastcgi 扩展
关于这个python-fastcgi C库的资料不太多,所以我想问一下有没有人能给我一个简单的例子,教我怎么用它做一个简单的FastCGI服务器。如果能给我一个“你好,世界”的例子就太好了。
2 个回答
3
我建议你使用一个快速的 fastcgi WSGI 包装器,比如这个,这样你一开始就不会被 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()
4
编辑:我之前理解错了问题。哎呀。
Jon的Python模块是一个有用模块的集合,其中包括一个很棒的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()