python-3.x - 如何让我的请求会话在超时时自动重试?
问题描述
我试图理解为什么我的请求会话在 20 秒后请求超时时不会自动重试。我的印象是我配置的重试设置会导致会话在超时时自动重试,但我得到以下异常:
# yada yada
socket.timeout: The write operation timed out
During handling of the above exception, another exception occurred:
# yada yada
urllib3.exceptions.ProtocolError: ('Connection aborted.', timeout('The write operation timed out',))
下面是我如何配置 Session 对象并发出请求的片段:
import requests
from requests.packages.urllib3.util.retry import Retry
from requests.adapters import HTTPAdapter
session = requests.Session()
settings = Retry( total = 10, backoff_factor = 0.1, status_forcelist = [ 500, 502, 503, 504 ] )
session.mount( "https://", HTTPAdapter( max_retries = settings ) )
resp = session.post( uploadAddress, files={"file": myFile}, timeout=20 )
我似乎无法在官方文档中找到有关将 Retry 对象与 timeout 参数结合使用的有用信息,因此可能无法简单地做到这一点。
解决方案
推荐阅读
- c# - Drools 不会向我的 c# 桌面应用程序返回任何结果
- uwp - 如何设置 InkCanvas 笔颜色的笔触不透明度?
- ember.js - 将 ember 应用程序升级到 3.0 意外令牌 <<
- keras - Keras 连接卷积层:每个卷积层所需的数据格式
- python-3.5 - 在帮助文档中显示 @property 数据描述符的类型提示
- html - 如何在文件名而不是 URL 中编码正斜杠 (/)?
- python - 我正在尝试通过在 Python 中组合 3 个列表来制作 3D 点列表
- vba - 如何在路径中编写带有变量的vba
- python - Python 元素树语法错误:无法分配给文字。
- angular - Angular 2+ HttpIntercept:在返回拦截函数之前等待来自服务器的令牌