The `requests` library supports automatic retries with exponential backoff

The Python requests library can automatically retry failed HTTP requests with exponential backoff using urllib3’s Retry class and a custom adapter.

Implementation:

import requests
from requests.adapters import HTTPAdapter
from urllib3.util import Retry

# Configure retry strategy
retries = Retry(
    total=3,                              # Max retry attempts
    backoff_factor=3,                     # Wait time multiplier
    status_forcelist=[500, 502, 503, 504] # Retry on these status codes
)

# Create adapter with retry configuration
adapter = HTTPAdapter(max_retries=retries)

# Apply to session
session = requests.Session()
session.mount('https://', adapter)
session.mount('http://', adapter)

# Make requests through the session
response = session.get("https://api.example.com/endpoint")

How exponential backoff works:

With backoff_factor=3, the wait time between retries increases exponentially:

Key parameters:

Use cases:

The session will automatically retry failed requests without additional code, making your HTTP clients more resilient to transient failures.

Original source