Just use requests'
features:
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)
This will GET
the URL and retry 3 times in case of requests.exceptions.ConnectionError
. backoff_factor
will help to apply delays between attempts to avoid to fail again in case of periodic request quota.
Take a look at requests.packages.urllib3.util.retry.Retry
, it has many options to simplify retries.