https://github.com/simonw/asgi-proxy-lib
An ASGI function for proxying to a backend over HTTP
https://github.com/simonw/asgi-proxy-lib
asgi httpx
Last synced: 4 days ago
JSON representation
An ASGI function for proxying to a backend over HTTP
- Host: GitHub
- URL: https://github.com/simonw/asgi-proxy-lib
- Owner: simonw
- License: apache-2.0
- Created: 2023-07-01T01:34:51.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2023-07-01T02:03:56.000Z (almost 2 years ago)
- Last Synced: 2024-10-18T07:53:34.661Z (6 months ago)
- Topics: asgi, httpx
- Language: Python
- Homepage:
- Size: 10.7 KB
- Stars: 21
- Watchers: 2
- Forks: 0
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-asgi - asgi-proxy-lib - An ASGI function for proxying to a backend over HTTP. (Proxies)
README
# asgi-proxy-lib
[](https://pypi.org/project/asgi-proxy-lib/)
[](https://github.com/simonw/asgi-proxy-lib/releases)
[](https://github.com/simonw/asgi-proxy-lib/blob/main/LICENSE)An ASGI function for proxying to a backend over HTTP
**⚠️ Warning: this is an early alpha.**
## Installation
Install this library using `pip`:
pip install asgi-proxy-lib
## Usage
This library provides a single ASGI function called `asgi_proxy`. You can use it like this:
```python
from asgi_proxy import asgi_proxyapp = asgi_proxy("https://datasette.io")
```
Now `app` is an ASGI application that will proxy all incoming HTTP requests to the equivalent URL on `https://datasette.io`.The function takes an optional second argument, `log=` - set this to a Python logger, or any object that has `.info(msg)` and `.error(msg)` methods, and the proxy will log information about each request it proxies.
It also takes a `timeout=` option, which defaults to `None` for no timeout. This can be set to a floating point value in seconds to enforce a timeout on requests that are being proxied.
## CLI tool
You can try this module out like so:
```bash
python -m asgi_proxy https://datasette.io
```
You may need to `pip install uvicorn` first for this to work.Alternatively, use [uv](https://github.com/astral-sh/uv) like this:
```bash
uv run --with uvicorn --with asgi-proxy-lib python -m asgi_proxy https://datasette.io
```This will start a server on port 8000 that proxies to `https://datasette.io`.
Add `-p PORT` to specify a different port, `--timeout 3` to set a timeout, `--verbose` to see debug logging, and `--host 127.0.0.1` to listen on a different host (the default is `0.0.0.0`).
## Development
To contribute to this library, first checkout the code. Then create a new virtual environment:
cd asgi-proxy-lib
python -m venv venv
source venv/bin/activateNow install the dependencies and test dependencies:
pip install -e '.[test]'
To run the tests:
pytest