Created
May 21, 2019 11:54
-
-
Save lorey/03cac43359984775686fbc88f2041857 to your computer and use it in GitHub Desktop.
Dealing with HTTPSConnectionPool errors in requests with adapters and backoff
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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