python-3.x - Python3 requests.get 太慢

编辑 添加信息:

请求版本:2.21.0

服务器信息: 一个 Windows python 实现,包括 10 个 threading.Thread 实例,每个创建 BaseHTTPRequestHandler 的处理程序的 strong>HTTPServer。我的 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)` 不会让它花费更长的时间。它似乎总是在返回结果之前等待整整一秒钟
  • 使用 session 包装器并取消保持事件状态并没有改善。我的意思是:
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
...

最佳答案

来自docs :

timeout is not a time limit on the entire response download; rather, an exception is raised if the server has not issued a response for timeout seconds (more precisely, if no bytes have been received on the underlying socket for timeout seconds). If no timeout is specified explicitly, requests do not time out.

https://stackoverflow.com/questions/55938825/

相关文章:

php - DomPDF 中的整页 3 列布局

kubernetes - 如何将 kubernetes pods 标签添加到普罗米修斯指标?

html - 在 <input> 上使用 onChange 时未注册空间

sparql - 维基数据导入 virtuoso

javascript - 在 iFrame 中禁用外部内容加载

reactjs - 如何修复 VSCode 中的 "Expression expected. ts(

laravel - 登录 laravel 背包后有条件地重定向用户

javascript - 从 useReducer Hook 返回的状态是 "deep copy"还

git - 如何修复 git bash 错误异常 : STATUS_ACCESS_VIOLATION

php - Composer 更新给出错误说插件无法初始化,找不到每个包的类