gevent.WSGIServer 请求方法之谜

1 投票
1 回答
1386 浏览
提问于 2025-04-17 10:50

我在运行gevent的WSGIServer时遇到了一些非常奇怪的情况。似乎每一个请求的方式都被错误地解读了。

如果我发送以下请求:

requests.get('http://localhost:5000')
requests.head('http://localhost:5000')
requests.delete('http://localhost:5000')
requests.put('http://localhost:5000')
requests.post('http://localhost:5000')

这就是在控制台中出现的内容:

127.0.0.1 - - [2012-01-22 14:55:36] "POST / HTTP/1.1" 405 183 "-" "python-requests/0.9.1"
127.0.0.1 - - [2012-01-22 14:55:41] "DELETE / HTTP/1.1" 405 185 "-" "python-requests/0.9.1"
127.0.0.1 - - [2012-01-22 14:55:46] "16 / HTTP/1.1" 405 181 "-" "python-requests/0.9.1"
127.0.0.1 - - [2012-01-22 14:55:50] "8 / HTTP/1.1" 405 180 "-" "python-requests/0.9.1"
127.0.0.1 - - [2012-01-22 14:56:13] "HEAD / HTTP/1.1" 200 0 "-" "python-requests/0.9.1"

为了完整起见,这就是我正在运行的脚本:

from gevent.wsgi import WSGIServer
from flask import Flask

app = Flask(__name__)
app.debug = True

@app.route("/")
def hello():
    return 'hello'

port = 5000

http_server = WSGIServer(('', port), app)
http_server.serve_forever()

这可能是什么原因呢?

补充:

我使用的gevent版本是:0.13.0

1 个回答

1

Libevent对HTTP方法的支持是有限的,而且支持哪些HTTP方法取决于你使用的libevent版本。如果你看到的是一个数字而不是方法名,那显然是个错误。可能是你在构建和链接gevent时使用了不同的版本。

你可以试试切换到gevent.pywsgi,这样可以解决这个问题,不过可能会牺牲一些性能。

另外,gevent的1.0版本有很多很棒的改进。你可以在这里下载:http://code.google.com/p/gevent/downloads/list

撰写回答