问题概述
我正在运行一个网页和一个用 python 编写的 API。网页在 POST 请求中将一些数据发送到 python API,后者对其进行处理并将其发回。该 API 适用于 curl 请求和控制台 AJAX 请求。但是,当我在新选项卡中加载网页时,前 10 秒左右的所有 POST 请求都会“停止”(不是由 Chrome 执行),直到它们失败。大约 10 秒后,POST 请求工作正常,python API 捕获了一个损坏的管道(ERRNO 32,下面的日志)。如果我硬刷新选项卡,那么该站点会立即运行,并且请求根本不会停止,但是如果我打开一个带有该站点的新选项卡,那么就会有 10 秒的延迟。
细节
这只发生在 Chrome 上,它在 Firefox 和 Safari 上运行良好。我已经在 localhost 和 AWS 实例上运行了 API 和网页,但仍有约 10 秒的延迟,之后它工作正常。它发生在其他计算机上的 chrome 上,并且适用于其他计算机上的 Firefox。
我是如何尝试修复它的
当 Chrome 协商代理连接或达到 6 个 TCP 连接的限制时,Chrome 显然会停止请求。我不知道为什么进行硬重置不会导致它必须重新协商其与 API 的连接,或者为什么不必每次都这样做。即使它正在这样做,它也不应该持续花费大约 10 秒吗?我还确保该站点没有达到 6 个 TCP 连接。
潜在问题?
如果我将请求更改为来自另一个站点,我对 HTTP 请求停止没有问题,而且这个问题是 Chrome 特有的,所以我想知道 Chrome 是否以 BaseHTTPRequests 不知道如何处理的方式实现 XMLHTTPRequests ,这导致管道破裂。但我不知道为什么它总是在每次约 10 秒后自行修复。
我现在已经花了很多时间,任何帮助将不胜感激!
HTML/JS:
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8>
<title>OSR Web</title>
</head>
<body>
<script>
var http = new XMLHttpRequest();
var url = "http://localhost:1234";
http.onreadystatechange = function() {
if(http.readyState == 4 && http.status == 200) {
var bestGuess = http.responseText;
console.log(bestGuess);
}
}
document.addEventListener("click", function(event){
http.open("POST", url, true);
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.send("TestData");
});
</script>
</body>
</html>
Python API:
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
class S(BaseHTTPRequestHandler):
def _set_headers(self):
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.send_header('Access-Control-Allow-Origin', '*')
self.end_headers()
def do_GET(self):
self._set_headers()
self.wfile.write("OSR API Active")
def do_POST(self):
print "received"
self._set_headers()
self.data_string = self.rfile.read(int(self.headers['Content-Length']))
inputData = self.data_string
print inputData
self.wfile.write("Received\n")
def run(server_class=HTTPServer, handler_class=S, port=1234):
server_address = ('', port)
httpd = server_class(server_address, handler_class)
print 'Starting HTTPD'
httpd.serve_forever()
run()
断管错误信息:
127.0.0.1 - - [11/Aug/2017 15:34:13] "GET / HTTP/1.1" 200 -
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 56506)
Traceback (most recent call last):
File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 295, in _handle_request_noblock
self.process_request(request, client_address)
File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 321, in process_request
self.finish_request(request, client_address)
File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 334, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 657, in __init__
self.finish()
File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 716, in finish
self.wfile.close()
File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 283, in close
self.flush()
File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 307, in flush
self._sock.sendall(view[write_offset:write_offset+buffer_size])
error: [Errno 32] Broken pipe
----------------------------------------
received
127.0.0.1 - - [11/Aug/2017 15:36:45] "POST / HTTP/1.1" 200 -
TestData