HTTP traffic capture middleware for Python web applications
Project description
kusho-capture
Kusho-capture is a lightweight HTTP traffic capture middleware for Python web applications. It seamlessly integrates with popular frameworks like FastAPI, Flask, and Django to record API traffic for testing, debugging, and monitoring purposes.
Features
- 🔄 Automatic HTTP traffic capture for API endpoints
- 🎯 Configurable URL pattern matching
- 📦 Support for both WSGI and ASGI applications
- 🔍 Detailed request/response logging
- ⚡ Async support for modern web frameworks
- 🎛️ Configurable sampling rate for high-traffic applications
- 📊 Batch processing of captured events
- 🚀 Framework auto-detection
Installation
pip install kusho-capture
For framework-specific dependencies:
pip install kusho-capture[fastapi] # For FastAPI support
pip install kusho-capture[flask] # For Flask support
pip install kusho-capture[django] # For Django support
Quick Start
FastAPI Example
from fastapi import FastAPI
from kusho_capture import EventCollector, setup_traffic_capture
app = FastAPI()
collector = EventCollector(
collector_url="https://your-collector-endpoint.com",
sample_rate=0.1,
batch_size=100
)
app = setup_traffic_capture(app, collector, framework="fastapi")
@app.get("/api/items")
async def get_items():
return {"items": ["item1", "item2"]}
Flask Example
from flask import Flask
from kusho_capture import EventCollector, setup_traffic_capture
app = Flask(__name__)
collector = EventCollector(
collector_url="https://your-collector-endpoint.com",
sample_rate=0.1,
batch_size=100
)
app.wsgi_app = setup_traffic_capture(app.wsgi_app, collector, framework="flask")
@app.route("/api/items")
def get_items():
return {"items": ["item1", "item2"]}
Django Example
# settings.py
MIDDLEWARE = [
'kusho_capture.WSGIMiddleware',
# ... other middleware
]
# somewhere in your configuration
from kusho_capture import EventCollector, setup_traffic_capture
collector = EventCollector(
collector_url="https://your-collector-endpoint.com",
sample_rate=0.1,
batch_size=100
)
application = setup_traffic_capture(application, collector, framework="django")
Configuration
EventCollector Options
collector_url: URL of your event collection endpointbatch_size: Number of events to batch before sending (default: 100)flush_interval: Maximum time to wait before sending a batch in seconds (default: 60)max_queue_size: Maximum number of events to queue (default: 10000)sample_rate: Percentage of requests to capture (default: 0.1 = 10%)
Middleware Options
url_patterns: List of URL patterns to match for capture (default: ['/api/'])framework: Auto-detected by default, can be explicitly set to 'fastapi', 'flask', or 'django'
Event Data Structure
Captured events include:
- Timestamp
- Request path and method
- Request headers
- Query parameters
- Request body (for POST/PUT/PATCH requests)
- Response status and headers
- Response time
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Project details
Release history Release notifications | RSS feed
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 kusho_capture-0.1.0.tar.gz.
File metadata
- Download URL: kusho_capture-0.1.0.tar.gz
- Upload date:
- Size: 6.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7d7e925e781d3e3cbb17ef0a2d67397aeebb5cba37f4427a0efc865940d046c4
|
|
| MD5 |
2f9bccfbf57c42351a23efeb4a998ffd
|
|
| BLAKE2b-256 |
1a35411bb1fa50a3cbfd2bca1838cceed1856135003ef81b203ddabbb9db8b84
|
File details
Details for the file kusho_capture-0.1.0-py3-none-any.whl.
File metadata
- Download URL: kusho_capture-0.1.0-py3-none-any.whl
- Upload date:
- Size: 6.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
396748912921fb95b9d9bb9f9f9c3c09cc517a1852d893096a86f5619f4061c7
|
|
| MD5 |
def0fbc4df664f13db4355adae723692
|
|
| BLAKE2b-256 |
cad3c34db2dd25823e8dc0c2c78d21fe0247c76fc942b1d72bdd70f8743efcc0
|