Requests Ratelimiter

0.9.3 · active · verified Mon Apr 13

Requests-Ratelimiter is a Python library that provides easy-to-use rate-limiting capabilities for the popular `requests` library. It acts as a wrapper around `pyrate-limiter`, implementing the leaky bucket algorithm to control the rate of HTTP requests. It is currently at version 0.9.3 and generally follows the release cadence of its underlying `pyrate-limiter` dependency, with updates typically driven by new features or fixes in the core rate-limiting logic.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `LimiterSession` as a drop-in replacement for `requests.Session` to apply a simple rate limit. It also shows how to define more complex rate limits using `RequestRate` and `Duration` from the underlying `pyrate-limiter` library. The `httpbin.org/get` endpoint is used for demonstration purposes.

import os
from requests_ratelimiter import LimiterSession
from time import time

session = LimiterSession(per_second=5) # Limit to 5 requests per second
start = time()

for i in range(10):
    try:
        response = session.get('https://httpbin.org/get') # Or any other API endpoint
        response.raise_for_status()
        print(f'[t+{time()-start:.2f}] Sent request {i+1} (Status: {response.status_code})')
    except Exception as e:
        print(f'Request {i+1} failed: {e}')

# Example with custom rate using pyrate-limiter objects
from pyrate_limiter import RequestRate, Duration, Limiter
# Limit to 10 requests per minute
custom_limiter_session = LimiterSession(limiter=Limiter(RequestRate(10, Duration.MINUTE)))
start_custom = time()
for i in range(5):
    try:
        response = custom_limiter_session.get('https://httpbin.org/get')
        response.raise_for_status()
        print(f'[t+{time()-start_custom:.2f}] Sent custom-limited request {i+1} (Status: {response.status_code})')
    except Exception as e:
        print(f'Custom-limited request {i+1} failed: {e}')

view raw JSON →