Skip to content

Instantly share code, notes, and snippets.

@mariocesar

mariocesar/script.py

Created Sep 30, 2020
Embed
What would you like to do?
Python requests timeout retry
import requests
from requests.adapters import Retry, HTTPAdapter
class RetryTimeoutAdapter(HTTPAdapter):
def __init__(self):
max_retries = Retry(
total=3,
backoff_factor=1,
status_forcelist=[500, 502, 503, 504],
method_whitelist=["GET", "HEAD", "INFO"],
)
super().__init__(max_retries=max_retries)
class Session(requests.Session):
def __init__(self):
super().__init__()
self.mount("https://", RetryTimeoutAdapter())
self.mount("http://", RetryTimeoutAdapter())
self.headers.update(
{
"Pragma": "no-cache",
"Cache-Control": "no-cache",
}
)
with Session() as session:
response = session.get("myurltotimeout")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment