Bit*_*nce 9 ajax keep-alive werkzeug flask
我有一个jQuery Ajax调用,如下所示:
$("#tags").keyup(function(event) {
$.ajax({url: "/terms",
type: "POST",
contentType: "application/json",
data: JSON.stringify({"prefix": $("#tags").val() }),
dataType: "json",
success: function(response) { display_terms(response.terms); },
});
Run Code Online (Sandbox Code Playgroud)
我有一个像这样的Flask方法:
@app.route("/terms", methods=["POST"])
def terms_by_prefix():
req = flask.request.json
tlist = terms.find_by_prefix(req["prefix"])
return flask.jsonify({'terms': tlist})
Run Code Online (Sandbox Code Playgroud)
tcpdump显示HTTP对话框:
POST /terms HTTP/1.1
Host: 127.0.0.1:5000
User-Agent: Mozilla/5.0 (X11; Linux i686; rv:12.0) Gecko/20100101 Firefox/12.0
Accept: application/json, text/javascript, */*; q=0.01
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive
Content-Type: application/json; charset=UTF-8
X-Requested-With: XMLHttpRequest
Referer: http://127.0.0.1:5000/
Content-Length: 27
Pragma: no-cache
Cache-Control: no-cache
{"prefix":"foo"}
Run Code Online (Sandbox Code Playgroud)
但是,Flask回复没有keep-alive.
HTTP/1.0 200 OK
Content-Type: application/json
Content-Length: 445
Server: Werkzeug/0.8.3 Python/2.7.2+
Date: Wed, 09 May 2012 17:55:04 GMT
{"terms": [...]}
Run Code Online (Sandbox Code Playgroud)
是不是真的没有实现keep-alive?
fai*_*ker 22
默认的request_handler是WSGIRequestHandler.
之前app.run(),添加一行,
WSGIRequestHandler.protocol_version = "HTTP/1.1"
别忘了from werkzeug.serving import WSGIRequestHandler.
Werkzeug的集成Web服务器构建在Python标准库的BaseHTTPServer之上.如果将HTTP协议版本设置为1.1,BaseHTTPServer似乎支持Keep-Alives.
Werkzeug没有这样做但是如果你准备好攻击Flask用来实例化Werkzeug的BaseWSGIServer的机器,你可以自己动手.看看Flask.run()哪个电话werkzeug.serving.run_simple().你需要做的就是归结为BaseWSGIServer.protocol_version = "HTTP/1.1".
我没有测试过这个解决方案.我想你确实知道Flask的web服务器应该只用于开发.
| 归档时间: |
|
| 查看次数: |
10378 次 |
| 最近记录: |