Skip to content

Instantly share code, notes, and snippets.

@lorey
Created May 21, 2019 11:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lorey/03cac43359984775686fbc88f2041857 to your computer and use it in GitHub Desktop.
Save lorey/03cac43359984775686fbc88f2041857 to your computer and use it in GitHub Desktop.
Dealing with HTTPSConnectionPool errors in requests with adapters and backoff
# this snippet will deal with errors like HTTPSConnectionPool: Max retries exceeded with url...
# by using a backoff factor
# further reading:
# - docs: https://2.python-requests.org/en/master/user/advanced/#transport-adapters
# - stack overflow issue: https://stackoverflow.com/a/47475019
import requests
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util.retry import Retry
session = requests.Session()
retry = Retry(connect=3, backoff_factor=0.5)
adapter = HTTPAdapter(max_retries=retry)
session.mount('http://', adapter)
session.mount('https://', adapter)
session.get(url)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment