def retry_request(n_retries=5, **kwargs):
 exc = resp = None
 for n_try in range(n_retries):
  try:
   exc = None
   resp = requests.request(**kwargs)
   if (resp.status_code < 500):
    return resp
  except requests.RequestException as exc:
   pass
  time.sleep(((2 ** (n_try + 1)) * 0.5))
 if exc:
  raise exc
 if resp:
  resp.raise_for_status()
 raise Exception('An unknown problem occurred with a request.')

def retry_request ( n_retries = 5 , ** kwargs ) : exc = resp = None for n_try in range ( n_retries ) : try : exc = None resp = requests . request ( ** kwargs ) if ( resp . status_code < 500 ) : return resp except requests . RequestException as exc : pass time . sleep ( ( ( 2 ** ( n_try + 1 ) ) * 0.5 ) ) if exc : raise exc if resp : resp . raise_for_status ( ) raise Exception ( 'An unknown problem occurred with a request.' )
retry a requests request with exponential backoff up to n_retries times .