Total Pageviews

Saturday 24 November 2012

以fastcgi方式自动启动python脚本的配置

网上很多关于python脚本以fastcgi方式在lighttpd中运行的配置,都是python以独立进程方式手动运行,如:
fastcgi.server              = ( "/beylog.fcgi" =>
                               ( "main" =>
                                 (
                                   "host" => "127.0.0.1",
                                   "port" => 3333,
                                   "check-local" => "disable",
                                  )
                                )
                              )
而php的fastcgi方式运行,一般都是由lighttpd自动启动的。如:
fastcgi.server = ( ".php" => ((
                     "bin-path" => "/usr/bin/php-cgi",
                     "socket" => "/tmp/php.socket"
                 )))
那python脚本是否也能像php一样方便呢?最近重新翻出flup中一直搞不明白的代码,发现,python脚本使用flup,也能使用像php一样的配置:
fastcgi.server=(
"/hello/app.fcgi" =>(
"main" => (
    "bin-path" => "/usr/bin/python /www/wwwroot/hello/app.fcgi",
    "socket" => "/tmp/lighttpd.fcgi.hello.socket",
    "check-local" => "disable",
    )
),
)
如上,bin-path设置启动的命令;socket设置到/tmp目录里面(lighttpd有权创建编辑文件),这个socket只是一个必须的摆设,实际是用过fastcgi标准中定义的FCGI_LISTENSOCK_FILENO来通讯;
app.fcgi脚本中,flup server以参数bindAddress=None方式启动,如果是直接使用flup包,代码:
from flup.server.fcgi import WSGIServer
WSGIServer(AppHandle).run()
如果是django项目,代码:
from django.core.servers.fastcgi import runfastcgi
runfastcgi(method="threaded", daemonize="false")
这样,python脚本也想php脚本一样,由lighttpd fastcgi模块自动启动管理。
----------------------------------------------------------------------------------------