One*_*nly 3 python python-3.x python-requests
我正在使用 requests post 发送一些数据,现在我已将 的超时值设置requests.post为类似60. 其他类似的问题与特定的应用程序相关,所以我想将其作为通用的 python 错误来问。
这是我得到的错误:
failed to connect ('Connection aborted.', timeout('The write operation timed out'))
Run Code Online (Sandbox Code Playgroud)
我认为这是因为由于某种原因我的线程无法及时发送所有数据,但我不确定。所以我有以下问题:
发生这个错误是因为线程无法在超时时间内发送出所有数据吗?
如果是这样,我是否可以以仅在发送所有数据后才开始的方式设置超时?我不想永远等待服务器响应,但显然我希望计时器在发送所有数据时启动,而不是在函数调用开始时启动。(并且我发送的数据大小各不相同)
小智 7
如果您使用的是python-request 库,则可以将“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
Run Code Online (Sandbox Code Playgroud)
requests 在底层使用 urrlib3.util.Timeout 。 根据 urllib3 文档,将读取超时设置为 None 将永远等待服务器响应。但是,我同意永远等待是一个坏主意。
您在请求中发送了多少数据?我通读了一些 python 请求代码,但找不到一种简单的方法来检测请求何时完成发送。
作为概念证明,我根据urequests(微请求)编写了这段代码
它允许您将回调函数传递给 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)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15618 次 |
| 最近记录: |