Retry Requests

2.0.0 · active · verified Wed Apr 15

retry-requests is a Python library that enhances `requests.Session` objects to automatically retry failed HTTP requests. It handles transient issues like connection errors, timeouts, and specific HTTP response codes (5XX and 3XX by default) with exponential backoff. Currently at version 2.0.0, the library is actively maintained, with releases as needed based on contributions and bug fixes.

Warnings

Install

Imports

Quickstart

Demonstrates how to create a retry-enabled requests Session using the `retry` function, both with default settings and with custom retry attempts and backoff factor. It shows how the session automatically retries on specified HTTP status codes (like 503) or connection errors.

from retry_requests import retry
from requests import Session

# Basic usage with default retries (3 retries, exponential backoff)
my_session = retry()
try:
    response = my_session.get("https://httpbin.org/status/503") # Will retry on 503
    response.raise_for_status()
    print(f"Success after retries: {response.status_code}")
except Exception as e:
    print(f"Request failed after all retries: {e}")

# Customizing retries and backoff
# Example: 5 retries, backoff factor of 0.2
custom_session = retry(Session(), retries=5, backoff_factor=0.2)
try:
    response = custom_session.get("https://httpbin.org/status/503")
    response.raise_for_status()
    print(f"Success after custom retries: {response.status_code}")
except Exception as e:
    print(f"Request failed after custom retries: {e}")

view raw JSON →