Skip to content

Instantly share code, notes, and snippets.

@saveshodhan
Created August 19, 2019 14:10
Show Gist options
  • Save saveshodhan/34ccf67d1aa9f3f2801a0b5a4e95b217 to your computer and use it in GitHub Desktop.
Save saveshodhan/34ccf67d1aa9f3f2801a0b5a4e95b217 to your computer and use it in GitHub Desktop.
Using the "Retry" feature of the Requests module
import requests
from requests.adapters import HTTPAdapter
from urllib3.util.retry import Retry # or, from requests.packages.urllib3.util.retry import Retry
## to get rid of that SSL insecure warning ##
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
def requests_retry(url, total=3, backoff_factor=0.3, method_whitelist=None, status_forcelist=None):
"""Create and return a Retry object
:param url: URL (prefix) to register
:param total: (optional) Total number of retries to allow. Defaults to 5
:param backoff_factor: (optional) a factor to decide how much time to wait before the next retry (see [1] below). Defaults to 0.3
:param method_whitelist: (optional) Methods to allow for retry. Defaults to Retry.DEFAULT_METHOD_WHITELIST which is frozenset(['HEAD', 'TRACE', 'GET', 'PUT', 'OPTIONS', 'DELETE'])
:param status_forcelist: (optional) HTTP statuses to force a retry on. Defaults to 500, 501
:return: session object
:rtype: <class 'requests.sessions.Session'>
[1] https://urllib3.readthedocs.io/en/latest/reference/urllib3.util.html#module-urllib3.util.retry
"""
ret = Retry(
total = total,
backoff_factor = backoff_factor,
method_whitelist = method_whitelist or Retry.DEFAULT_METHOD_WHITELIST,
status_forcelist = status_forcelist or [500, 501]
)
s = requests.Session()
s.mount(url, HTTPAdapter(max_retries = ret))
return s
"""
Example:
>>> import logging
>>> logging.basicConfig(level=logging.DEBUG)
>>> API_URL = 'https://httpbin.org'
>>> s = requests_retry(API_URL)
>>> s.get(API_URL + '/status/500', verify=False)
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): httpbin.org
DEBUG:urllib3.connectionpool:https://httpbin.org:443 "GET /status/500 HTTP/1.1" 500 0
DEBUG:urllib3.util.retry:Incremented Retry for (url='/status/500'): Retry(total=2, connect=None, read=None, redirect=None, status=None)
DEBUG:urllib3.connectionpool:Retry: /status/500
DEBUG:urllib3.connectionpool:https://httpbin.org:443 "GET /status/500 HTTP/1.1" 500 0
DEBUG:urllib3.util.retry:Incremented Retry for (url='/status/500'): Retry(total=1, connect=None, read=None, redirect=None, status=None)
DEBUG:urllib3.connectionpool:Retry: /status/500
DEBUG:urllib3.connectionpool:https://httpbin.org:443 "GET /status/500 HTTP/1.1" 500 0
DEBUG:urllib3.util.retry:Incremented Retry for (url='/status/500'): Retry(total=0, connect=None, read=None, redirect=None, status=None)
DEBUG:urllib3.connectionpool:Retry: /status/500
DEBUG:urllib3.connectionpool:https://httpbin.org:443 "GET /status/500 HTTP/1.1" 500 0
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/ssave/venv/local/lib/python2.7/site-packages/requests/sessions.py", line 521, in get
return self.request('GET', url, **kwargs)
File "/home/ssave/venv/local/lib/python2.7/site-packages/requests/sessions.py", line 508, in request
resp = self.send(prep, **send_kwargs)
File "/home/ssave/venv/local/lib/python2.7/site-packages/requests/sessions.py", line 618, in send
r = adapter.send(request, **kwargs)
File "/home/ssave/venv/local/lib/python2.7/site-packages/requests/adapters.py", line 499, in send
raise RetryError(e, request=request)
requests.exceptions.RetryError: HTTPSConnectionPool(host='httpbin.org', port=443): Max retries exceeded with url: /status/500 (Caused by ResponseError('too many 500 error responses',))
"""
@saveshodhan
Copy link
Author

saveshodhan commented Aug 19, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment