Async http client/server framework

Asynchronous HTTP client/server framework for asyncio and Python.

Key Features

  • Supports both client and server side of HTTP protocol.
  • Supports both client and server Web-Sockets out-of-the-box and avoids
    Callback Hell.
  • Provides Web-server with middlewares and pluggable routing.

Getting started


To get something from the web:

.. code-block:: python

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, '')

if name == 'main':
loop = asyncio.get_event_loop()


An example using a simple server:

.. code-block:: python

# examples/
from aiohttp import web

async def handle(request):
    name = request.match_info.get('name', "Anonymous")
    text = "Hello, " + name
    return web.Response(text=text)

async def wshandle(request):
    ws = web.WebSocketResponse()
    await ws.prepare(request)

    async for msg in ws:
        if msg.type == web.WSMsgType.text:
            await ws.send_str("Hello, {}".format(
        elif msg.type == web.WSMsgType.binary:
            await ws.send_bytes(
        elif msg.type == web.WSMsgType.close:

    return ws

app = web.Application()
app.add_routes([web.get('/', handle),
                web.get('/echo', wshandle),
                web.get('/{name}', handle)])

if __name__ == '__main__':