Set a retry strategy for Python requests


If you're overly enthuiastic with your requests to a server, it can get passive aggressive and give you the silent treatment or get overwhelmed and ask for a vacation. To mitigate that, give it some space with a retry strategy. Here's one such strategy with Python's requests package.

import requests
from requests.adapters import HTTPAdapter, Retry

max_retries = Retry(
    total=3,
    allowed_methods=False,
    status_forcelist=[408, 429, 500, 502, 503, 504],
    backoff_factor=1,
)
adapter = HTTPAdapter(max_retries=max_retries)
http = requests.Session()
http.mount("https://", adapter)
http.mount("http://", adapter)

resp = http.get("https://i.imgur.com/AD3MbBi.jpeg")

References