Reputation: 12380
I have found next example
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)
But it still fails if i get 502
errors for a while (server is restarting for 10 seconds).
Upvotes: 7
Views: 13635
Reputation: 276306
You are passing max_retries to HTTPAdapter which does not recover from 502 errors (by design) since it's not a connection error (but an applicative one). You can instead pass retries to http.request. See the retrying docs.
Upvotes: 5