我正在尝试用Python实现重试请求。
它对.get()
请求的工作方式与charm类似,但无论状态代码如何,都不会重试.post()
请求。我想将它与.post()
请求一起使用。
我的代码:
from requests.packages.urllib3.util import Retry
from requests.adapters import HTTPAdapter
from requests import Session, exceptions
s = Session()
s.mount('http://', HTTPAdapter(max_retries=Retry(total=2, backoff_factor=1, status_forcelist=[ 500, 502, 503, 504, 521])))
r = s.get('http://httpstat.us/500')
r2 = s.post('http://httpstat.us/500')
因此,.get()
请求会重试,.post()
请求不会重试。
怎么了?
在urllib3中,默认情况下不允许将
POST
作为重试方法(因为它可能导致多次插入)。你可以强迫它:见https://urllib3.readthedocs.io/en/latest/reference/urllib3.util.html#urllib3.util.retry.Retry
相关问题 更多 >
编程相关推荐