编辑:添加信息:
请求 版本:2.21.0
服务器信息: 是Windows python实现,其中包括10个 threading.Thread 实例,每个实例创建< strong> HTTPServer ,并具有基于 BaseHTTPRequestHandler 的处理程序。我的do_GET看起来像这样:
def do_GET(self):
rc = 'some response'
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.send_header('Access-Control-Allow-Origin', '*')
self.end_headers()
self.wfile.write(rc.encode('utf-8'))
我的行为很奇怪。
使用curl
命令行 , 快速完成GET命令: >
curl "http://localhost:3020/pbio/button2?cmd=uz-crosslink-leds&g1=0&g2=0&g3=0&g4=1&tmr=1"
但是 ,使用requests.get()
的python 会花费太多时间 。我被隔离到
python -c "import requests; requests.get('http://localhost:3020/pbio/button2?cmd=uz-crosslink-leds&g1=0&g2=0&g3=0&g4=1&tmr=1')"
我在这里浏览了许多其他问题,并且尝试了很多事情,但都没有成功。
这是我的一些发现:
timeout=0.2
,则通话将快速结束,而不会出现任何错误。timeout=5
或timeout =(5,5)`不会花费更长的时间。在返回结果之前,似乎总是要等一整秒钟。with requests.Session() as session:
session.headers.update({'Connection': 'close'})
url = "http://localhost:3020/pbio/button2?cmd=uz-crosslink-leds&g1=0&g2=0&g3=0&g4=%d&tmr=0" % i
session.get(url, timeout=2)
url=http://localhost:3020/pbio/button2?cmd=uz-crosslink-leds&g1=0&g2=0&g3=0&g4=1&tmr=0 DEBUG:urllib3.connectionpool:Starting new HTTP connection (1): localhost:3020 send: b'GET /pbio/button2?cmd=uz-crosslink-leds&g1=0&g2=0&g3=0&g4=1&tmr=0 HTTP/1.1\r\nHost: localhost:3020\r\nUser-Agent: python-requests/2.21.0\r\nAccept-Encoding: gzip, deflate\r\nAccept: */*\r\nConnection: close\r\n\r\n' reply: 'HTTP/1.0 200 OK\r\n' header: Server: BaseHTTP/0.6 Python/3.7.2 header: Date: Wed, 01 May 2019 15:28:29 GMT header: Content-type: text/html header: Access-Control-Allow-Origin: * DEBUG:urllib3.connectionpool:http://localhost:3020 "GET /pbio/button2?cmd=uz-crosslink-leds&g1=0&g2=0&g3=0&g4=1&tmr=0 HTTP/1.1" 200 None url=http://localhost:3020/pbio/powermtr?cmd=read-power-density DEBUG:urllib3.connectionpool:Resetting dropped connection: localhost slight pause here send: b'GET /pbio/powermtr?cmd=read-power-density HTTP/1.1\r\nHost: localhost:3020\r\nUser-Agent: python-requests/2.21.0\r\nAccept-Encoding: gzip, deflate\r\nAccept: */*\r\nConnection: close\r\n\r\n' reply: 'HTTP/1.0 200 OK\r\n' header: Server: BaseHTTP/0.6 Python/3.7.2 header: Date: Wed, 01 May 2019 15:28:30 GMT header: Content-type: text/html header: Access-Control-Allow-Origin: * DEBUG:urllib3.connectionpool:http://localhost:3020 "GET /pbio/powermtr?cmd=read-power-density HTTP/1.1" 200 None 6.710,i=4 url=http://localhost:3020/pbio/button2?cmd=uz-crosslink-leds&g1=0&g2=0&g3=0&g4=4&tmr=0 DEBUG:urllib3.connectionpool:Resetting dropped connection: localhost slight pause here ...
答案 0 :(得分:-1)
来自docs:
timeout不是整个响应下载的时间限制;相反,如果服务器在超时秒内未发出响应(更确切地说,在超时秒内未在基础套接字上接收到任何字节),则会引发异常。 如果未明确指定超时,则请求不会超时。