I am using Python requests to make a post request.
I am trying to do something like this as shown in below post:
Retry with requests
When there is connection error or response status code received is from status_forcelist, it should retry(which is working fine). What I want to do is after first try (before retrying), I want to do some other stuff. This can be possible if I can catch Exception and handle it to do other stuff. But it seems that requests is not raising any exception in case of connection error or response code is in status_forcelist unless retry count reaches to max configured. How can I achieve this?
Here is the code sample:
def requests_retry_session(
retries=3,
backoff_factor=0.3,
status_forcelist=(500, 502, 504),
session=None,
):
session = session or requests.Session()
retry = Retry(
total=retries,
read=retries,
connect=retries,
backoff_factor=backoff_factor,
status_forcelist=status_forcelist,
)
adapter = HTTPAdapter(max_retries=retry)
session.mount('http://', adapter)
session.mount('https://', adapter)
return session
def do_something_more():
## do something to tell user API failed and it will retry
print("I am doing something more...")
Usage...
t0 = time.time()
try:
response = requests_retry_session().get(
'http://localhost:9999',
)
except Exception as x:
# Catch exception when connection error or 500 on first attempt and do something more
do_somthing_more()
print('It failed :(', x.__class__.__name__)
else:
print('It eventually worked', response.status_code)
finally:
t1 = time.time()
print('Took', t1 - t0, 'seconds')
I know exception will be raised after max allowed attempts(defined in retries=3). All I want is some signal from requests or urllib3 to tell my main program that first attempt is failed and now it will start retrying. So that my program can do something more based on it. If not through exception, something else.