WSGIProxy2
WSGIProxy2 is a Python library that provides a WSGI application to proxy HTTP requests to another server, supporting various HTTP client backends like requests, urllib3, or httpx. It allows for flexible routing and handling of incoming requests before forwarding them to a specified target URI. The current version is 0.5.1, and the project is maintained with infrequent but steady updates.
Common errors
-
ModuleNotFoundError: No module named 'wsgiproxy2'
cause Attempting to import `Proxy` directly from the `wsgiproxy2` package name, instead of the correct `wsgiproxy` module name.fixChange the import statement to `from wsgiproxy import Proxy`. -
TypeError: __init__() missing 1 required positional argument: 'target_uri'
cause The `Proxy` class constructor requires at least two arguments: the local path to proxy, and the target URI.fixEnsure you pass both the `local_path` and `target_uri` when instantiating the `Proxy` object, e.g., `application = Proxy('/my-path', 'http://example.com/api')`. -
requests.exceptions.ConnectionError: HTTPConnectionPool(host='localhost', port=8000): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at ...>: Failed to establish a new connection: [Errno 111] Connection refused'))cause The `target_uri` specified for the proxy is unreachable, or the backend server is not running/listening on the specified address and port.fixVerify that the `target_uri` is correct and that the backend server it points to is actively running and accessible from where the `wsgiproxy2` application is hosted.
Warnings
- gotcha The package is installed as `wsgiproxy2` but the primary class `Proxy` is imported from the `wsgiproxy` module. Using `from wsgiproxy2 import Proxy` will result in a `ModuleNotFoundError` or `AttributeError`.
- gotcha While `requests` is installed by default as the backend, to use `urllib3` or `httpx`, you must install `wsgiproxy2` with the corresponding extra dependencies (e.g., `pip install wsgiproxy2[httpx]`).
- gotcha By default, `wsgiproxy2` will propagate backend errors (e.g., connection refused, 404 from target) as the proxy's response. For more robust applications, you may need to implement custom error handling or retry logic using middleware.
Install
-
pip install wsgiproxy2 -
pip install wsgiproxy2[httpx]
Imports
- Proxy
from wsgiproxy2 import Proxy
from wsgiproxy import Proxy
Quickstart
from wsgiproxy import Proxy
from werkzeug.serving import run_simple
import os
# Configure the target URI for the proxy
TARGET_URI = os.environ.get('WSGIPROXY_TARGET_URI', 'http://localhost:8000')
# Create a Proxy instance, mapping '/proxy-path' to TARGET_URI
# All requests to '/proxy-path' will be forwarded to TARGET_URI
application = Proxy(
'/proxy-path',
TARGET_URI
)
if __name__ == '__main__':
print(f"Proxying requests from http://localhost:5000/proxy-path to {TARGET_URI}")
# Run the WSGI application using Werkzeug's development server
run_simple('localhost', 5000, application)