Skip to main content

Adapter to running ASGI applications on aiohttp

Project description

aiohttp-asgi

PyPI - License Wheel PyPI PyPI Coverage Status tests

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)

# Mount startup and shutdown events from aiohttp to ASGI app
asgi_resource.lifespan_mount(aiohttp_app)

# 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


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.6.1.tar.gz (10.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

aiohttp_asgi-0.6.1-py3-none-any.whl (16.0 kB view details)

Uploaded Python 3

File details

Details for the file aiohttp_asgi-0.6.1.tar.gz.

File metadata

  • Download URL: aiohttp_asgi-0.6.1.tar.gz
  • Upload date:
  • Size: 10.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.13.2 Darwin/24.4.0

File hashes

Hashes for aiohttp_asgi-0.6.1.tar.gz
Algorithm Hash digest
SHA256 bc86dec9c0e4aac75d82997ea8201c7ea7443841f168f997da7716d0d4def9d5
MD5 f04f3e7db97d29764c29130dfe4298cc
BLAKE2b-256 a7695d1d9308932508996fa5c87dc72b3c4c017ffee1e2aa1c44102bcb480878

See more details on using hashes here.

File details

Details for the file aiohttp_asgi-0.6.1-py3-none-any.whl.

File metadata

  • Download URL: aiohttp_asgi-0.6.1-py3-none-any.whl
  • Upload date:
  • Size: 16.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.13.2 Darwin/24.4.0

File hashes

Hashes for aiohttp_asgi-0.6.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6419fb2be43b3c9163973804d853fc8f4a04fa93a1f31e7065f358c68891144d
MD5 272316f7bcf297e9ea2cea2d42bb8765
BLAKE2b-256 916e35a4d9243339a18517b0ffd3539cc7ca4f29ac816da32a02a4540e1c70aa

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page