Javascript 从Chrome到服务器的请求花费的时间太长
首先,这些都是在我的机器上本地运行的。我向python脚本发出了一个AJAX请求,脚本在端口上侦听请求。我已经在safari、firefox和chrome上测试过了。使用safari和firefox,所有请求都会立即发送到端口,并将响应发送回浏览器。然而,对于chrome,从发出请求到python脚本看到请求之间需要20秒。如果我在chrome设置中关闭“预取资源以更快地加载页面”,那么python脚本会立即接收请求。发生了什么事 python文件:Javascript 从Chrome到服务器的请求花费的时间太长,javascript,python,ajax,google-chrome,networking,Javascript,Python,Ajax,Google Chrome,Networking,首先,这些都是在我的机器上本地运行的。我向python脚本发出了一个AJAX请求,脚本在端口上侦听请求。我已经在safari、firefox和chrome上测试过了。使用safari和firefox,所有请求都会立即发送到端口,并将响应发送回浏览器。然而,对于chrome,从发出请求到python脚本看到请求之间需要20秒。如果我在chrome设置中关闭“预取资源以更快地加载页面”,那么python脚本会立即接收请求。发生了什么事 python文件: import http.server imp
import http.server
import socketserver
PORT = 80
class MyHandler(http.server.BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.send_header("Content-Type", "text/plain")
self.end_headers()
self.wfile.write(b"My message\n")
httpd = socketserver.TCPServer(("", PORT), MyHandler)
print("serving at port", PORT)
httpd.serve_forever()
javascript文件:
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", "http://127.0.0.1:80", true);
xmlhttp.send();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
var data = xmlhttp.responseText;
console.log(data);
}
};
在最后一个
self.wfile.write
之后添加self.wfile.close()
,然后python脚本抱怨BrokenPipeError:[Errno 32]管道损坏和值错误:对关闭的文件执行I/O操作。此外,虽然浏览器仍会收到响应,但需要18-20秒,大部分时间是在发出请求的浏览器和在端口上看到请求的python服务器脚本之间。无法检查本地,因此您可以尝试self.finish()
而不是close()
?相同的错误。我不确定这是chrome问题还是我的脚本问题。