Proxy.py
Proxy.py is a lightweight, dependency-optional, and pluggable framework for building fast proxy servers, web servers, and pub/sub systems. It supports HTTP, HTTPS, TLS interception, DNS-over-HTTPS, and can function as a reverse or forward proxy. Currently at version 2.4.10, the library maintains an active release cadence with frequent updates and new features.
Warnings
- deprecated Usage of `ssl.wrap_socket` is deprecated in favor of `SSLContext.wrap_socket` for better security and future compatibility.
- gotcha TLS (HTTPS) interception requires the `cryptography` Python package and a functional `openssl` command-line utility in your system's PATH. Certificate management (generating root CA, signing server certificates) is also crucial and must be properly configured.
- gotcha The library heavily relies on a pluggable architecture. To implement custom logic (e.g., modifying requests, logging, authentication), you must create and register plugins correctly. Misconfigured or missing plugins can lead to unexpected proxy behavior or unhandled requests.
- gotcha The Grout feature, which offers ngrok-like functionality for exposing local services, requires specific configuration and understanding of its tunneling mechanics. Improper setup can expose services insecurely or fail to establish tunnels.
- gotcha Many features (e.g., dashboard, metrics, SSH proxy) are gated behind optional `extras` during installation. If functionality is missing or imports fail, it's often due to not installing the required extras.
Install
-
pip install proxy.py -
pip install proxy.py[dashboard,tls,grout,ssh]
Imports
- Proxy
from proxy.py import Proxy
- HttpProxyPlugin
from proxy.py.http.proxy import HttpProxyPlugin
- http
from proxy.py import http
Quickstart
import os
from proxy.py import Proxy
if __name__ == '__main__':
# Example: Run a basic HTTP proxy on localhost:8899
args = [
'--hostname', '127.0.0.1',
'--port', '8899',
'--num-workers', '1', # For a simple quickstart
'--disable-web-server', # Disable built-in web server if only proxy is needed
'--plugins', 'proxy.py.http.proxy.HttpProxyPlugin' # Explicitly enable HTTP proxy
]
print(f"Starting HTTP proxy on 127.0.0.1:8899...")
print("Use Ctrl+C to stop.")
try:
# Proxy.main() takes arguments similar to command-line execution
with Proxy.main(args):
# The proxy runs in separate worker threads within the context manager.
# Keep the main thread alive to allow the proxy to run.
import time
while True:
time.sleep(1)
except KeyboardInterrupt:
print("\nProxy stopped.")
except Exception as e:
print(f"An error occurred: {e}")