首页 > 解决方案 > 重试配置仅在方法中指定时有效,但在 Poolmanager 中指定时无效

问题描述

文档说:

如果您希望所有请求都遵循相同的重试策略,您可以在 PoolManager 级别指定重试。

但如果我尝试:

import urllib3


http = urllib3.PoolManager(retries=False)

r = http.request("GET", "https://nghttp2.org/httpbin/redirect/4")
print(r.status)

我得到:

urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='nghttp2.org', port=443): Max retries exceeded with url: https://nghttp2.org/httpbin/relative-redirect/1 (Caused by ResponseError('too many redirects'))

如果在方法中指定了重试,一切都会按预期进行:

import urllib3
from urllib3.util.retry import Retry

http = urllib3.PoolManager()

r = http.request("GET", "https://nghttp2.org/httpbin/redirect/4", retries=Retry(connect=5, read=2, redirect=5))
print(r.status)

蟒蛇 3.9.7,urllib 1.26.7

标签: pythonurllib3

解决方案


推荐阅读