nginx配置python的cgi

来源:互联网 发布:软件模式流程 编辑:程序博客网 时间:2024/05/21 18:10

nginx不支持cgi,也就是nginx 不能直接执行外部脚本。但是nginx支持fastcgi。所以为了在nginx运行fastcgi,我们采用flup,flup是fastcgi的一种实现。这里是官网https://www.saddi.com/software/flup/ 我用最简单的模式就是 nginx+flup。
* 第一步安装flup。当前目录转为\python安装目录\Lib\site-packages 但后用easy_install安装flup的egg文件。
* 第二步就是配置nginx.conf文件,我用最简单的配置:

    location ~ ^/test {        include fastcgi_params;        fastcgi_pass 127.0.0.1:10080;    }

* 第三步也是最蛋疼的一部就是测试代码。用的pytho你上的示例代码:

from cgi import escape

import sys, os
from flup.server.fcgi import WSGIServer

def app(environ, start_response):
start_response(‘200 OK’, [(‘Content-Type’, ‘text/html’)])

yield '<h1>FastCGI Environment</h1>'yield '<table>'for k, v in sorted(environ.items()):     yield '<tr><th>%s</th><td>%s</td></tr>' % (escape(k), escape(v))yield '</table>'WSGIServer(app).run()

结果一直提示错误:

sock = socket.fromfd(FCGI_LISTENSOCK_FILENO, socket.AF_INET,AttributeError: 'module' object has no attribute 'fromfd'

网上也有说要修改fcgi_base.py的,我觉得不对,于是找到有人在最后一行代码中指定地址和端口。之前也一直在像端口在哪里设置。修改之后正常运行,至少能监听我要的端口。
最后一行修改为:

WSGIServer(app, bindAddress=('127.0.0.1',10080)).run()

估计这就是最简单的python+fcgi模式。Django之类的估计是在flup上的加强版本。不懂。

0 1
原创粉丝点击