RetryHTTP

1.4.0 · active · verified Fri Apr 10

RetryHTTP is a Python library designed to simplify retrying potentially transient HTTP errors. It extends the `tenacity` library with custom retry and wait strategies, offering sensible defaults while remaining fully customizable. The library provides decorators to automatically retry HTTP requests in applications using `requests`, `httpx`, or `aiohttp`.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use the `@retry` decorator with `httpx`. The decorated function will automatically retry on transient HTTP status codes (429, 500, 502, 503, 504), network errors, and timeouts, with appropriate backoff strategies. The `raise_for_status()` call is crucial to convert HTTP error responses into exceptions that `retryhttp` can intercept and retry.

import httpx
from retryhttp import retry

@retry
def fetch_data_with_retries():
    """Fetches data from example.com, retrying on transient HTTP errors."""
    print("Attempting to fetch data...")
    try:
        response = httpx.get("https://example.com/")
        response.raise_for_status() # Raise an exception for HTTP errors (4xx or 5xx)
        print("Successfully fetched data!")
        return response.text
    except httpx.HTTPStatusError as e:
        print(f"HTTP error occurred: {e.response.status_code}")
        raise # Re-raise to trigger retry logic if it's a retryable status
    except httpx.RequestError as e:
        print(f"Request error occurred: {e}")
        raise # Re-raise to trigger retry logic on network errors/timeouts

if __name__ == "__main__":
    data = fetch_data_with_retries()
    # In a real scenario, you'd process 'data' here
    # For this example, we just show successful fetch or final failure

view raw JSON →