我有一个用web.py编写的简单服务器。
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Unicode
from __future__ import unicode_literals
import sys
reload(sys)
# -----
# Logging
import logging
logging.basicConfig(level=logging.INFO)
# -----
# Libs
import web
import json
# -----
urls = ("/", "Index",
"/endpoint1", "EP1",
"/endpoint2", "EP2")
app = web.application(urls, globals())
class Index(object):
def GET(self):
return "Index"
def POST(self):
data = web.data()
web.header('Content-Type', 'application/json')
try:
result = json.loads(data)
except:
return "BAD JSON Index"
logging.info("[Server] Index " + json.dumps(result))
return "Index POST"
class EP1(object):
def GET(self):
return "EP1"
def POST(self):
data = web.data()
web.header('Content-Type', 'application/json')
try:
result = json.loads(data)
except:
return "BAD JSON EP1"
logging.info("[Server] EP1 " + json.dumps(result))
return "EP1 POST"
class EP2(object):
def GET(self):
return "EP2"
def POST(self):
data = web.data()
web.header('Content-Type', 'application/json')
try:
result = json.loads(data)
except:
return "BAD JSON EP2"
logging.info("[Server] EP2 " + json.dumps(result))
return "EP2 POST"
if __name__ == "__main__":
logging.info("[Server] Starting server.")
web.wsgi.runwsgi = lambda func, addr=None: web.wsgi.runfcgi(func, addr)
app.run()
在添加web.wsgi之前,我可以使用python server.py 0.0.0.0:7331
运行它并且它可以工作,我可以访问它。
问题是,它只适用于我的机器。其他机器没有看到服务器。
我认为这是因为我没有使用nginx或apache。所以,我跟进了this nginx tutorial。
我安装了所有要求,我正在运行nginx。我还修改了它的配置,但添加了教程中描述的location / {}
。此外,我在服务器中添加了listen 7331;
。
我的nginx配置如下所示:
user www-data;
worker_processes 4;
pid /run/nginx.pid;
events {
worker_connections 768;
}
http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
gzip on;
gzip_disable "msie6";
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
server {
listen 7331;
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 7331;
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 127.0.0.1:9002;
}
}
}
还有一些其他行,但它们被注释掉了,所以我删除了它们。
对于fastcgi_param SERVER_PORT 7331;
我也试过了$server_port
现在,我无法使用python server.py 0.0.0.0:7331
启动我的服务器,因为我收到了以下错误。
WSGIServer: missing FastCGI param REQUEST_METHOD required by WSGI!
WSGIServer: missing FastCGI param SERVER_NAME required by WSGI!
WSGIServer: missing FastCGI param SERVER_PORT required by WSGI!
WSGIServer: missing FastCGI param SERVER_PROTOCOL required by WSGI!
Status: 404 Not Found
Content-Type: text/html
从这样的开头开始,也不起作用。
spawn-fcgi -d /path/to/www -f /path/to/www/server.py -a 0.0.0.0 -p 7331
它说它正确启动,并为我提供服务器进程的PID。我可以用ps aux找到这个过程。但是,如果我尝试连接到my.external.ip.number:7331/
或my.external.ip.number:7331/endpoint*
它不起作用,它只会尝试连接很长时间,最终会失败。
所以,现在我描述了这个问题,我的问题是:
感谢您的帮助。