我找到了下一个示例
def prepare_retry_requester(retries: int = 5, forcelist: List = (503,)) -> requests.Session:
requester = requests.Session()
retry = urllib3.Retry(total=retries, backoff_factor=1, status_forcelist=forcelist)
for protocol in 'http://', 'https://':
requester.mount(protocol, requests.adapters.HTTPAdapter(max_retries=retry))
return requester
with prepare_retry_requester(forcelist=[502, 503, 413]) as requester:
response = requester.post(url, data=serialized)
但是,如果我在一段时间内遇到502
错误(服务器重新启动10秒钟),它仍然会失败。
答案 0 :(得分:1)
您正在将max_retries传递给HTTPAdapter,该适配器不能从502错误中恢复(通过设计),因为这不是连接错误(而是适用的错误)。您可以改为将重试传递给http.request。参见retrying docs。