A powerful async library to fetch URLs with strict rate limiting (requests per second).
Project description
HTTP Wizz 🧙♂️
HTTP Wizz is a Python library designed for high-performance, rate-limited URL fetching. Whether you're scraping data, calling third-party APIs with strict limits, or building robust microservices, HTTP Wizz provides the tools to handle "requests per second" (RPS) constraints with ease.
Key Features
- ⏱️ Strict Rate Limiting: Precisely control your throughput with "requests per second" (RPS).
- 🔄 Automatic Retries: Built-in resilience for flaky networks and unstable APIs.
- 📈 Exponential Backoff: Space out retries to avoid overwhelming target servers.
- 🎯 Custom Retry Logic: Force retries based on response content, even if the status code is
200 OK. - 🪄 Versatile API: Choose between a simple sync function, a high-level async client, or a low-level
aiohttpwrapper.
Installation
pip install http-wizz
Usage Guide
1. The Simple Way (Synchronous)
Perfect for quick scripts. Use fetch_urls to get results immediately without asyncio boilerplate.
from http_wizz import fetch_urls
urls = [f"https://api.example.com/data/{i}" for i in range(10)]
# Fetch at 5 requests per second
results = fetch_urls(urls, requests_per_second=5)
2. The Wizard Way (Async Client)
Best for async applications. WizzClient handles rate limiting, retries, and JSON parsing automatically.
import asyncio
from http_wizz import WizzClient
async def main():
urls = ["https://api.example.com/item/1", "https://api.example.com/item/2"]
client = WizzClient(requests_per_second=10)
results = await client.fetch_all(urls)
print(f"Fetched {len(results)} results")
asyncio.run(main())
3. The Power User Way (RateLimitedSession)
A drop-in replacement for aiohttp.ClientSession. Use this when you need full control over headers, cookies, or different HTTP methods (POST, PUT, etc.).
import asyncio
from http_wizz import RateLimitedSession
async def main():
async with RateLimitedSession(requests_per_second=2) as session:
for i in range(5):
async with session.get(f"https://httpbin.org/get?id={i}") as resp:
data = await resp.json()
print(f"Status {resp.status}: {data['args']}")
asyncio.run(main())
Advanced Configuration
Configuring Retries and Backoff
Both WizzClient and fetch_urls support deep customization of retry behavior.
from http_wizz import WizzClient
client = WizzClient(
requests_per_second=5, # Max throughput
max_retries=5, # Number of retry attempts
initial_retry_delay=1.0, # Seconds to wait after first failure
exponential_backoff=True # Double the delay after each failure (1s, 2s, 4s...)
)
Custom Retry Conditions
Sometimes APIs return 200 OK but include an error message in the body. You can force a retry by providing a should_retry callback.
def check_for_api_errors(response, content):
# Retry if the API returned a 'try_again' flag in the JSON body
return isinstance(content, dict) and content.get("status") == "try_again"
results = fetch_urls(
["https://api.example.com/job"],
should_retry=check_for_api_errors,
max_retries=10
)
API Reference
fetch_urls(...)
urls: List of strings.requests_per_second: (float) Max requests/sec. Default10.max_retries: (int) Default5.initial_retry_delay: (float) Default1.0.exponential_backoff: (bool) DefaultTrue.should_retry:callable(response, content) -> bool.
WizzClient(...)
- High-level async client. Same arguments as
fetch_urls. fetch_all(urls): Asynchronously fetches all provided URLs.
RateLimitedSession(...)
requests_per_second: (float) Max requests/sec.- All other arguments are passed to the underlying
aiohttp.ClientSession.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file http_wizz-0.1.0.tar.gz.
File metadata
- Download URL: http_wizz-0.1.0.tar.gz
- Upload date:
- Size: 8.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.12.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0799d751f84e4fc7c1013241938f2377d04b79bc2432fab8a6e8bd4890d051bd
|
|
| MD5 |
255db312160d6c3adcb684209e026646
|
|
| BLAKE2b-256 |
de1c9b77c95765e928458c0a32e0921c80fd7e44a26baa624c21e7cd32921897
|
File details
Details for the file http_wizz-0.1.0-py3-none-any.whl.
File metadata
- Download URL: http_wizz-0.1.0-py3-none-any.whl
- Upload date:
- Size: 6.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.12.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8cd489611bff105d65e4d703397e5baffe41e60a8446c8eaea1149964f8da598
|
|
| MD5 |
44821294f6d47640cecd465452d27b20
|
|
| BLAKE2b-256 |
5177431ed6da9cfc3ea9fb19f9235a2abd2c64402ae79eef9b2aa77ffbe79be6
|