§ HTTP client & server for asyncio
· asyncio 초기 기여자인 Andrew Svetlov가 주도하여 개발한 프레임워크
· server 코드는 Flask와 유사한 느낌으로 개발 가능
aiohttp
import aiohttp
import asyncio
async def fetch(session, url):
async with session.get(url) as response:
return await response.text()
async def main():
async with aiohttp.ClientSession() as session:
html = await fetch(session,
'https://python.org')
print(html)
asyncio.run(main())
from aiohttp import web
async def handle(request):
name = request.match_info.get('name',
"Anonymous")
text = "Hello, " + name
return web.Response(text=text)
app = web.Application()
app.add_routes([web.get('/', handle),
web.get('/{name}', handle)])
web.run_app(app)
https://aiohttp.readthedocs.io/en/stable/