An ASGI middleware for dynamic reverse proxy
Project description
Asgiproxify
Asgiproxify is an ASGI middleware for dynamic reverse proxy.
Installation
Of course, you can directly install from PyPI.
(venv) $ pip install asgiproxify
Getting Started
You can easily make a running example from these lines:
import uvicorn
from asgiproxify import Asgiproxify
proxify = Asgiproxify()
uvicorn.run(proxify)
Oh, you have an ASGI server already? You can set a fallback:
import uvicorn
from app import app
from asgiproxify import Asgiproxify
proxify = Asgiproxify(app) # or proxify.to(app)
uvicorn.run(proxify)
Adding a new reverse proxy endpoint is as simple as:
import uvicorn
from asgiproxify import Asgiproxify, AsgiproxifyHandler
proxify = Asgiproxify()
@proxify.register('/example/')
class FooProxyHanlder(AsgiproxifyHandler):
pass
uvicorn.run(proxify)
This will register a handler to handle all requests to /example/*
.
By default, an AsgiproxifyHandler will proxy any request to example.org.
AsgiproxifyHandler can be smarter than that, like proxy to any URL you wish, like this:
import uvicorn
from asgiproxify import Asgiproxify, AsgiproxifyHandler
proxify = Asgiproxify()
@proxify.register('/LICENSE')
class FooProxyHanlder(AsgiproxifyHandler):
def make_request_url(self):
return 'https://www.gnu.org/licenses/gpl-3.0.txt'
uvicorn.run(proxify)
You may dynamically make revrese proxy by reading self.scope. Its content is specified in ASGI Specification.
A little example for that will be:
import uvicorn
from asgiproxify import Asgiproxify, AsgiproxifyHandler
proxify = Asgiproxify()
@proxify.register('/')
class FooProxyHanlder(AsgiproxifyHandler):
def make_request_url(self):
return 'https://ftp.gnu.org' + self.scope['path']
uvicorn.run(proxify)
You can do more with it by utilizing the scope and other infomation.
The parameters you can tweek from the hander are request_url
,
request_cookies
, request_headers
and response_headers
. If that's
not enough for you, you can override make_request
which will allow
you to create your own aiohttp request!
You can consult proxify.py
for more technical infomation.
License
This project is proudly licensed in GNU General Public License v3 or later.
Please refer to COPYING
for more infomation.
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
File details
Details for the file asgiproxify-0.0.1.tar.gz
.
File metadata
- Download URL: asgiproxify-0.0.1.tar.gz
- Upload date:
- Size: 16.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 56ebbff09e9dfa4b051f82f598c273fd8bac5a11011cac4e3def86c781027a4d |
|
MD5 | 296af40ffc517fddf9b5b46918bee5d8 |
|
BLAKE2b-256 | e0347736b41265a5eb4ecd28ac433bf1e9f72797c7339d43a6ed3d948124b07f |
File details
Details for the file asgiproxify-0.0.1-py3-none-any.whl
.
File metadata
- Download URL: asgiproxify-0.0.1-py3-none-any.whl
- Upload date:
- Size: 17.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4252b89b0ee99c4a9c30f57d570220e9295bb37aa5e44cfcc9c05b8ea211ad39 |
|
MD5 | 8270092cb52936f0b57d0d667c82ec5e |
|
BLAKE2b-256 | c0ad2e533a074f455f89d6f5d7b70cb9ed9c911ef68187feb12ed9c6a8052ca8 |