我编写了一个基本的Python脚本webapp.py以供测试:
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
import sys, os, traceback
from html import escape
from flup.server.fcgi import WSGIServer
def app(environ, start_response):
start_response('200 OK', [('Content-Type', 'text/html')])
yield 'hello world'
WSGIServer(app, bindAddress=('127.0.0.1',3001)).run()我可以启动脚本./webapp.py,没问题。我还可以通过telnet建立到localhost的连接:3001。
然后创建一个Nginx默认配置,如下所示:
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
server_name _;
location / {
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_script_name;
fastcgi_pass localhost:3001;
}
}在我的本地机器上使用这种简单的配置,我启动了nginx并尝试访问http://localhost。Nginx默认站点失败(502坏网关)。在日志消息中,我只能反复看到这样的错误:
2017/01/05 01:23:07 [error] 30464#30464: *3 upstream prematurely closed connection while reading response header from upstream, client: 127.0.0.1, server: _, request: "GET / HTTP/1.1", upstream: "fastcgi://127.0.0.1:3001", host: "localhost"我的设置或代码出了什么问题?
发布于 2017-01-05 10:17:41
是我的错
如果你像我一样,跟随Python3的官方如何在网络上使用Python,安装了氟化物-吡啶1.0.2-1并感到困惑。不要这样。这不是你的错。
上面的代码和设置没有什么问题。问题是我如何安装flup-py3。我就是这样安装的:
sudo pip3 install flup-py3你不应该做的事。
解决方案
你的问题(和我的问题)的答案很简单。“稳定”版本1.0.2-1根本不适用于Python 3。所以这位官员大惊小怪地说了一句。
在更新pypi上的官方稳定版本之前,您应该通过以下方式安装flup-py3:
sudo pip3 install hg+https://hg.saddi.com/flup-py3.0/#egg=flup-py3或者:
sudo pip3 install git+https://github.com/pquentin/flup-py3.git#egg=flup-py3第一个(hg)将是最初开发人员Allan的fluppy3的开发版本。而第二个版本(github版本)是由昆廷·普拉德特创建的副本。这两个来源都包括修复flup兼容性问题的2012-2014年补丁。
相关链接
2018年最新情况**
最新版本的flup-py3 (>=1.0.3)已经解决了这个问题。你可以正常地用pip安装它。
https://stackoverflow.com/questions/41469810
复制相似问题