【问题标题】:Python error with request post : 'Connection aborted, timeout('The write operation timed out')请求发布的 Python 错误:'连接中止,超时('写入操作超时')
【发布时间】:2021-01-07 13:43:30
【问题描述】:

我正在使用 requests post 发送一些数据,现在我已将 requests.post 的超时值设置为 60 之类的值。其他与特定应用程序相关的类似问题,所以我想将其作为通用 python 错误来问。

这是我得到的错误:

failed to connect  ('Connection aborted.', timeout('The write operation timed out'))

我认为这是因为由于某种原因我的线程无法及时发送所有数据,但我不确定所以只是想检查一下。所以:

  1. 出现这个错误是因为线程无法在超时时间内发送所有数据吗?

  2. 如果是这样,我是否可以以仅在我发出所有数据后才开始的方式设置超时?我不想等待服务器响应,但显然我希望计时器在我发送所有数据时启动,而不是在函数调用开始时启动。 (而且我发送的数据大小不一)

【问题讨论】:

    标签: python python-3.x python-requests


    【解决方案1】:

    如果你使用python-request library,你可以将'timeout'参数指定为一个元组。第一个元素是“连接超时”,第二个是“读取超时”。

    requests.get('http://google.com', timeout=(10,200) # give it 10 seconds to connect to the server, and timeout if server does not send any data back for 200+ seconds
    

    requests 在后台使用 urrlib3.util.Timeout。 Per the urllib3 docs,将读取超时设置为 None 将永远等待服务器响应。但是,我同意永远等待是个坏主意。

    您在请求中发送了多少数据?我通读了一些 python-requests 代码,但找不到一种简单的方法来检测请求何时完成发送。

    作为概念证明,我基于 urequests (micro requests) 编写了 this code

    它允许您将回调函数传递给 requests.get()。该回调会在所有请求数据发送完毕后,服务器没有收到任何数据之前触发。

    import time
    
    def sent_callback():
        print('The request has finished sending at', time.time())
    
    resp = get('http://google.com', send_callback=sent_callback)
    print('response received at', time.time())
    
    print('resp.text=', resp.text)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-14
      • 2015-08-14
      • 1970-01-01
      • 2015-03-20
      • 2023-03-26
      • 1970-01-01
      • 2018-06-26
      • 2017-07-13
      相关资源
      最近更新 更多