'Python requests & urllib3 Retry - How may retries were made?

Given following example usage:

adapter = HTTPAdapter(max_retries=Retry(
    total=5,
    backoff_factor=0.1,
    status_forcelist=[429, 500, 502, 503, 504],
    method_whitelist=["HEAD", "GET", "OPTIONS"]
))
session = requests.Session()
session.mount("http://", adapter)
session.mount("https://", adapter)
rsp = session.post(url, json=my_json, params=my_params)

How do I tell how many retries were made? I'm trying to debug/diagnose/resolve an issue posted in this related question

Alternatively, is there a different usage of these libs that provides this?



Solution 1:[1]

You can get the list of retries by accessing the retries.history property of the underlying urllib3.response.HTTPResponse object used by the requests library.

In your case, that would be:

rsp.raw.retries.history

Solution 2:[2]

In a worse case (if the first request will get status code in response 429, 500, 502, 503, 504), there will be 5 retries with a backoff factor of 0.1. You can look there the explanation of each parameter. https://urllib3.readthedocs.io/en/latest/reference/urllib3.util.html#module-urllib3.util.retry

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Enrico Marchesin
Solution 2 Alex Fedorov