Adapter to running ASGI applications on aiohttp
Project description
aiohttp-asgi
This module provides a way to use any ASGI compatible frameworks and aiohttp together.
Example
from aiohttp import web
from fastapi import FastAPI
from starlette.requests import Request as ASGIRequest
from aiohttp_asgi import ASGIResource
asgi_app = FastAPI()
@asgi_app.get("/asgi")
async def root(request: ASGIRequest):
return {
"message": "Hello World",
"root_path": request.scope.get("root_path")
}
aiohttp_app = web.Application()
# Create ASGIResource which handle
# any request startswith "/asgi"
asgi_resource = ASGIResource(
asgi_app,
root_path="/asgi"
)
# Register resource
aiohttp_app.router.register_resource(
asgi_resource
)
# [Optional]
asgi_resource.lifespan_mount(
aiohttp_app,
startup=True,
# By default starlette didn't
# handle "lifespan.shutdown"
shutdown=False,
)
# Start the application
web.run_app(aiohttp_app)
Installation
pip install aiohttp-asgi
ASGI HTTP server
Command line tool for starting aiohttp web server with ASGI app.
Example
Create the test_app.py
from starlette.applications import Starlette
from starlette.responses import JSONResponse
from starlette.routing import Route
async def homepage(request):
return JSONResponse({'hello': 'world'})
routes = [
Route("/", endpoint=homepage)
]
application = Starlette(debug=True, routes=routes)
and run the test_app.py
with aiohttp-asgi
aiohttp-asgi \
--address "[::1]" \
--port 8080 \
test_app:application
alternatively using python -m
python -m aiohttp_asgi \
--address "[::1]" \
--port 8080 \
test_app:application
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
aiohttp-asgi-0.3.1.tar.gz
(6.8 kB
view details)
Built Distribution
File details
Details for the file aiohttp-asgi-0.3.1.tar.gz
.
File metadata
- Download URL: aiohttp-asgi-0.3.1.tar.gz
- Upload date:
- Size: 6.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9b367d58f286a2cf741687d8fef1de2e357663bb92f89d284c6de76c647cd858 |
|
MD5 | 160c5fb0d71ed1208dc8552bb9d28d6a |
|
BLAKE2b-256 | 561b6823b8c8bcac2580997a3ab44ed42ca5e1c220d71795e9338aebad3f5b4b |
File details
Details for the file aiohttp_asgi-0.3.1-py3-none-any.whl
.
File metadata
- Download URL: aiohttp_asgi-0.3.1-py3-none-any.whl
- Upload date:
- Size: 15.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 132bcd5140db5e8ecdd02f047f0d0af45c3f7fe04b3b00e7c2b2f0976c40134f |
|
MD5 | 2e4a96293f82fbf0fe271393d3019ef4 |
|
BLAKE2b-256 | 08bb88a28ffa706deb77f0eedd098286f8f734a22e93b81acee4f894cb2aaaa2 |