import requests
from requests.adapters import HTTPAdapter
from urllib3 import Retry
DEFAULT_RETRIES = 5
DEFAULT_BACKOFF = 0.3
def session_with_retries(max_retries: int = DEFAULT_RETRIES,
backoff_factor: float = DEFAULT_BACKOFF,
proxies: dict = None) -> requests.Session:
new_session = requests.Session()
retries = Retry(total=max_retries,
connect=max_retries,
read=max_retries,
status=max_retries,
allowed_methods=frozenset(['HEAD', 'GET', 'POST']),
status_forcelist=frozenset([500, 502, 503, 504]),
backoff_factor=backoff_factor,
)
retry_adapter = HTTPAdapter(max_retries=retries)
new_session.mount('http://', retry_adapter)
new_session.mount('https://', retry_adapter)
if proxies is not None:
new_session.proxies.update(proxies)
return new_session
This code is for retry logic with python requests. And is working fine too. As expected it will throw exception on 503 status code after max_retries. So collectively it will throw exception for [500, 502, 503, 504].
How I can get the status code on which the exception was thrown from this method. As exception messages i could see something
HTTPSConnectionPool(host='some-host', port=443): Max retries exceeded with url: /services/data/v52.0/connect/communities?pageSize=100 (Caused by ResponseError('too many 503 error responses',))
I traced back the exceptions urllib3.exceptions.MaxRetryError, requests.exceptions.RetryError but could not find about its status code for exception.
Strictly NO for other libraries like tenacity