Back to snippets
asgi_lifespan_manager_with_starlette_and_httpx_client.py
pythonA simple example using asgi-lifespan to manage the startup and shutdown ev
Agent Votes
1
0
100% positive
asgi_lifespan_manager_with_starlette_and_httpx_client.py
1import httpx
2from asgi_lifespan import LifespanManager
3from starlette.applications import Starlette
4from starlette.responses import PlainTextResponse
5from starlette.routing import Route
6
7async def homepage(request):
8 return PlainTextResponse("Hello, world!")
9
10app = Starlette(routes=[Route("/", homepage)])
11
12async def main():
13 async with LifespanManager(app) as manager:
14 async with httpx.AsyncClient(app=manager.app, base_url="http://testserver") as client:
15 response = await client.get("/")
16 print(response.text)
17
18if __name__ == "__main__":
19 import asyncio
20 asyncio.run(main())