Skip to main content

在 aiohttp 上运行 ASGI 应用程序的适配器

项目描述

aiohttp-asgi

PyPI - 许可证 车轮 派皮 派皮 覆盖状态 毒物

该模块提供了一种将任何兼容 ASGI 的框架和 aiohttp 一起使用的方法。

例子

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)

安装

pip install aiohttp-asgi

ASGI HTTP 服务器

使用 ASGI 应用程序启动 aiohttp Web 服务器的命令行工具。

例子

创建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)

test_app.py并运行aiohttp-asgi

aiohttp-asgi \
    --address "[::1]" \
    --port 8080 \
    test_app:application

或者使用python -m

python -m aiohttp_asgi \
    --address "[::1]" \
    --port 8080 \
    test_app:application

下载文件

下载适用于您平台的文件。如果您不确定要选择哪个,请了解有关安装包的更多信息。

源分布

aiohttp-asgi-0.4.0.tar.gz (11.4 kB 查看哈希

已上传 source

内置分布

aiohttp_asgi-0.4.0-py3-none-any.whl (15.7 kB 查看哈希

已上传 py3