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

Viewed 1008

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?

2 Answers

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
Related