A blazing-fast HTTP proxy for Python, powered by Go’s fasthttp library
Project description
pygofastproxy
A blazing-fast HTTP proxy for Python, powered by Go's fasthttp library.
Quick Start
-
Install the package:
pip install pygofastproxy
-
Start your backend server (e.g., Flask) on port 4000.
-
Run the proxy with advanced options:
from pygofastproxy import run_proxy # Basic usage run_proxy(target="http://localhost:4000", port=8080) # High-performance configuration run_proxy( target="http://localhost:4000", port=8080, max_conns_per_host=2000, rate_limit_rps=5000, allowed_origins="https://myapp.com,https://admin.myapp.com" )
-
Send requests to
http://localhost:8080.
How it Works
pygofastproxy launches a Go-based HTTP proxy as a subprocess from Python. The Go proxy listens on the specified port and forwards all HTTP requests to your backend server. Configuration is handled via Python arguments or environment variables.
Overview
pygofastproxy is a Python package that provides a super-fast HTTP proxy, powered by Go, for use with Python web backends. It is ideal for scenarios where you want to:
- Add a high-performance reverse proxy in front of your Python (Flask, FastAPI, Django, etc.) backend.
- Integrate with frontend frameworks (like Next.js) that need to proxy API requests to a Python backend.
- Use as a development tool to forward requests, add logging, or simulate production-like proxying locally.
- Robust and Secure.
Features
- Ultra-fast HTTP proxying using Go's fasthttp library
- Simple Python API to launch and control the proxy
- Automatic Go binary build if not present
- Easily configurable target and port
- Advanced CORS handling with cached origin validation using
ALLOWED_ORIGINS - Built-in rate limiting to prevent overload
- Real-time metrics endpoint at
/__proxy_metrics - Configurable connection pooling for optimal performance
- Production-ready security headers
- Zero-allocation optimizations with byte-slice operations
- Environment variable configuration for production deployments
- More features coming soon!
Installation
You can install from PyPI:
pip install pygofastproxy
Or, for local development(whl):
pip install /path/to/pygofastproxy
Usage
As a Python Module
from pygofastproxy import run_proxy
# Start the proxy (forwards :8080 to your backend at :4000)
run_proxy(target="http://localhost:4000", port=8080)
- By default, the proxy will listen on
localhost:8080and forward to your backend atlocalhost:4000. - You can adjust the
targetandportas needed.
If you're using this in production and want to restrict allowed frontend domains, set the ALLOWED_ORIGINS environment variable:
export ALLOWED_ORIGINS=https://example.com,https://www.example.com
🚀 Performance Configuration
1. Connection Pooling
The proxy uses optimized connection pooling with configurable settings:
from pygofastproxy import run_proxy
# High-performance configuration
run_proxy(
target="http://localhost:4000",
port=8080,
max_conns_per_host=2000, # Increase for high traffic
read_timeout="5s", # Reduce for faster timeouts
write_timeout="5s"
)
2. Rate Limiting
Built-in rate limiting prevents overload:
run_proxy(
target="http://localhost:4000",
port=8080,
rate_limit_rps=2000 # Allow 2000 requests per second
)
3. Metrics and Monitoring
Access real-time metrics at /__proxy_metrics:
run_proxy(
target="http://localhost:4000",
port=8080,
enable_metrics=True # Enable metrics endpoint (default: True)
)
Example metrics response:
{
"requests": 15432,
"errors": 23,
"avg_duration_ms": 12.5,
"uptime_seconds": 3600,
"error_rate": 0.15
}
4. CORS Optimization
CORS origins are cached for better performance:
run_proxy(
target="http://localhost:4000",
port=8080,
allowed_origins="https://myapp.com,https://admin.myapp.com"
)
5. Production Configuration
For production environments, use these settings:
from pygofastproxy import run_proxy
run_proxy(
target="http://localhost:4000",
port=8080,
max_conns_per_host=5000,
read_timeout="10s",
write_timeout="10s",
rate_limit_rps=5000,
enable_metrics=True,
allowed_origins="https://yourdomain.com"
)
📊 Performance Benchmarks
With these optimizations, you can expect:
- Latency: Reduced by 30-50% due to optimized header operations
- Throughput: Increased by 40-60% with better connection pooling
- Memory: Reduced memory allocation with byte-slice operations
- CORS: 10x faster CORS handling with origin caching
Environment Variables
You can control the proxy using these environment variables:
PY_BACKEND_TARGET: The backend server URL to forward requests to (default:http://localhost:4000).PY_BACKEND_PORT: The port for the proxy to listen on (default:8080).ALLOWED_ORIGINS: Comma-separated list of allowed CORS origins (optional, used in production for CORS validation).
Performance Environment Variables
Configure via environment variables for production:
PROXY_MAX_CONNS_PER_HOST=2000
PROXY_READ_TIMEOUT=5s
PROXY_WRITE_TIMEOUT=5s
PROXY_RATE_LIMIT_RPS=2000
PROXY_ENABLE_METRICS=true
ALLOWED_ORIGINS="https://myapp.com,https://admin.myapp.com"
Testing
To manually test the proxy:
-
Start a backend server on port 4000 (e.g.,
python3 -m http.server 4000). -
Start the proxy as shown above.
-
In another terminal, run:
curl http://localhost:8080You should see the response from your backend, confirming the proxy is working.
📈 Monitoring and Troubleshooting
Monitoring
Monitor your proxy using the metrics endpoint:
curl http://localhost:8080/__proxy_metrics
Set up alerts for:
- High error rates (> 5%)
- High average latency (> 100ms)
- Rate limit triggers
Troubleshooting
- High latency: Reduce
read_timeoutandwrite_timeout - Connection errors: Increase
max_conns_per_host - Rate limiting: Adjust
rate_limit_rpsor implement client-side backoff - CORS issues: Verify
allowed_originsconfiguration
Example with Flask and Next.js (Dockerized)
Suppose you have a Flask backend and a Next.js frontend. You can use pygofastproxy as a reverse proxy between them:
- Frontend (Next.js) sends API requests to
localhost:8080 - pygofastproxy forwards requests to Flask backend at
localhost:4000
docker-compose.yml (simplified):
version: '3.8'
services:
proxy:
build: ./proxy
ports:
- "8080:8080"
depends_on:
- backend
backend:
build: ./backend
ports:
- "4000:4000"
frontend:
build: ./frontend
ports:
- "3000:3000"
environment:
- NEXT_PUBLIC_API_URL=http://localhost:8080
Use Cases
- Development proxy: Quickly forward requests from a frontend to a Python backend, simulating production proxying.
- Performance: Add a fast Go-based proxy in front of Python services for better throughput.
- API Gateway: Use as a lightweight API gateway for microservices.
- Testing: Intercept and forward requests for integration testing.
Requirements
- Python 3.7+
- Go (for building the proxy binary)
License
This project is licensed under the MIT License.
Contributing
Contributions are welcome! Please open issues or submit pull requests for bug fixes, improvements, or new features. For major changes, please open an issue first to discuss what you would like to change.
Credits
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 pygofastproxy-1.0.7.tar.gz.
File metadata
- Download URL: pygofastproxy-1.0.7.tar.gz
- Upload date:
- Size: 13.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f9b1d8e25adae013ec2d0a9b9ca7546215b25456ff0afad651619f0a8ac0445f
|
|
| MD5 |
96bde34b559cf6cfafb6d5516db6326d
|
|
| BLAKE2b-256 |
2960a9f248d1502945f68583191191337a83ead337ab67cee6a0b68485263f69
|
Provenance
The following attestation bundles were made for pygofastproxy-1.0.7.tar.gz:
Publisher:
python-publish.yml on 26zl/pygofastproxy
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pygofastproxy-1.0.7.tar.gz -
Subject digest:
f9b1d8e25adae013ec2d0a9b9ca7546215b25456ff0afad651619f0a8ac0445f - Sigstore transparency entry: 464503512
- Sigstore integration time:
-
Permalink:
26zl/pygofastproxy@9d85f5a0a2d8878833bb2d1340f589117adc5ea4 -
Branch / Tag:
refs/tags/v1.0.7 - Owner: https://github.com/26zl
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@9d85f5a0a2d8878833bb2d1340f589117adc5ea4 -
Trigger Event:
release
-
Statement type:
File details
Details for the file pygofastproxy-1.0.7-py3-none-any.whl.
File metadata
- Download URL: pygofastproxy-1.0.7-py3-none-any.whl
- Upload date:
- Size: 12.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
815bfd940d82ac7e8d26df6ab60347eed50143a4b0cc50f7cb9d6f59c5c94da1
|
|
| MD5 |
0dedbe163616225b0df7446afad80cc7
|
|
| BLAKE2b-256 |
6fbe60cac055302cf0c8b19da904af723754966a37f3a2c04de2cffe50d7cf03
|
Provenance
The following attestation bundles were made for pygofastproxy-1.0.7-py3-none-any.whl:
Publisher:
python-publish.yml on 26zl/pygofastproxy
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pygofastproxy-1.0.7-py3-none-any.whl -
Subject digest:
815bfd940d82ac7e8d26df6ab60347eed50143a4b0cc50f7cb9d6f59c5c94da1 - Sigstore transparency entry: 464503538
- Sigstore integration time:
-
Permalink:
26zl/pygofastproxy@9d85f5a0a2d8878833bb2d1340f589117adc5ea4 -
Branch / Tag:
refs/tags/v1.0.7 - Owner: https://github.com/26zl
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@9d85f5a0a2d8878833bb2d1340f589117adc5ea4 -
Trigger Event:
release
-
Statement type: