pyhaproxy
pyhaproxy is a minimal HAProxy-like reverse proxy and load balancer written in Python.
This is not a wrapper around the HAProxy binary. It is a pure Python async implementation focused on extensibility.
MVP Features
- Frontends — bind to multiple host:port pairs, each with its own routing rules
- ACL-like routing — route requests by
hostorpath_prefix - Backend pools — groups of servers behind a single backend name
- Load balancing — round-robin and least-connections
- Health checks — concurrent async HTTP checks per backend
- Prometheus metrics — request count and latency histograms
- Connection pooling — shared
httpx.AsyncClientwith keep-alive - X-Forwarded- headers* — automatically added to proxied requests
Quickstart
pip install pyhaproxy
Run
pyhaproxy run -c examples/config.yml
Test
curl -H "Host: example.com" http://localhost:9000/
curl -H "Host: example.com" http://localhost:9000/api/users
Example Config
frontends:
- name: "http_front"
bind: "0.0.0.0:9000"
default_backend: "app_backend"
rules:
- if: "host == example.com"
backend: "app_backend"
- if: "path_prefix == /api"
backend: "api_backend"
backends:
app_backend:
balance: "roundrobin"
servers:
- url: "http://localhost:5000"
- url: "http://localhost:5001"
api_backend:
balance: "leastconn"
servers:
- url: "http://localhost:6000"
healthcheck:
enabled: true
interval_seconds: 5
timeout_seconds: 2
path: "/health"
metrics:
enabled: true
path: "/metrics"
Architecture
Client ──► Uvicorn ──► Starlette ──► Frontend.resolve_backend()
│
┌─────▼──────┐
│ ACL rules │
│ host/path │
└─────┬──────┘
│ backend name
▼
BackendRegistry
│
┌─────▼──────┐
│ Balancer │
│ RR / Least │
└─────┬──────┘
│ server URL
▼
proxy.forward()
│
┌─────▼──────┐
│ Backend │
│ server │
└────────────┘
Changelog
0.1.1
- Multi-frontend support: CLI now runs all configured frontends concurrently.
- Connection pooling: Shared
httpx.AsyncClientwith keep-alive limits (was creating a client per request). - Concurrent health checks: Servers are checked in parallel via
asyncio.gather. - X-Forwarded- headers*: Proxied requests now include
x-forwarded-for,x-forwarded-proto,x-forwarded-host. - Better error messages: Balancer errors say "at least one server".
- Improved type annotations: Full type hints on all public classes.
- Comprehensive tests: Balancers, config, registry, frontend, ACL, proxy.
License
MIT
Metadata
Release files for pyhaproxy-lb 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| pyhaproxy_lb-0.1.1.tar.gz | 10.8 kB | Without provenance | Details for pyhaproxy_lb-0.1.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pyhaproxy_lb-0.1.1-py3-none-any.whl | Python 3 | none | any | Details for pyhaproxy_lb-0.1.1-py3-none-any.whl |
Total release size: 22.5 kB
Release files / pyhaproxy_lb-0.1.1.tar.gz
| Download URL | pyhaproxy_lb-0.1.1.tar.gz |
|---|---|
| Size | 10.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d46a357f60abf7670e52b8dcca8fa880116d2018cef43a455657936ce310bc2e
|
|
BLAKE2b-256 checksum How to use checksums |
046219beac7021eb1ee743bb7674dd50ad706bc16190c34cc660b914dfe848ab
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.10
|
Release files / pyhaproxy_lb-0.1.1-py3-none-any.whl
| Download URL | pyhaproxy_lb-0.1.1-py3-none-any.whl |
|---|---|
| Size | 11.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
30549153be80c8cb38d3e51d192440dabeaae25ba8411f1e5c2d04904434a89f
|
|
BLAKE2b-256 checksum How to use checksums |
bfae216085e33053493878e5a102680c5d187fedb68b66a0f1927e2b1f3a0da2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.10
|
Security for pyhaproxy-lb 0.1.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.