Skip to main content

http client/server for asyncio

Project description

http client/server for asyncio

aiohttp logo


  • Supports both client and server side of HTTP protocol.

  • Supports both client and server Web-Sockets out-of-the-box.

  • Web-server has middlewares and pluggable routing.

Getting started


To retrieve something from the web:

import aiohttp
import asyncio

async def fetch(session, url):
    with aiohttp.Timeout(10, loop=session.loop):
        async with session.get(url) as response:
            return await response.text()

async def main(loop):
    async with aiohttp.ClientSession(loop=loop) as session:
        html = await fetch(session, '')

if __name__ == '__main__':
    loop = asyncio.get_event_loop()


This is simple usage example:

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 wshandler(request):
    ws = web.WebSocketResponse()
    await ws.prepare(request)

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

    return ws

app = web.Application()
app.router.add_get('/echo', wshandler)
app.router.add_get('/', handle)
app.router.add_get('/{name}', handle)


Note: examples are written for Python 3.5+ and utilize PEP-492 aka async/await. If you are using Python 3.4 please replace await with yield from and async def with @coroutine e.g.:

async def coro(...):
    ret = await f()

should be replaced by:

def coro(...):
    ret = yield from f()


Discussion list

aio-libs google group:!forum/aio-libs


Optionally you may install the cChardet and aiodns libraries (highly recommended for sake of speed).


aiohttp is offered under the Apache 2 license.

Source code

The latest developer version is available in a github repository:


If you are interested in by efficiency, AsyncIO community maintains a list of benchmarks on the official wiki:


1.3.1 (2017-02-09)

  • Handle CLOSING in WebSocketResponse.__anext__

  • Fixed AttributeError ‘drain’ for server websocket handler #1613

1.3.0 (2017-02-08)

  • Multipart writer validates the data on append instead of on a request send #920

  • Multipart reader accepts multipart messages with or without their epilogue to consistently handle valid and legacy behaviors #1526 #1581

  • Separate read + connect + request timeouts # 1523

  • Do not swallow Upgrade header #1587

  • Fix polls demo run application #1487

  • Ignore unknown 1XX status codes in client #1353

  • Fix sub-Multipart messages missing their headers on serialization #1525

  • Do not use readline when reading the content of a part in the multipart reader #1535

  • Add optional flag for quoting FormData fields #916

  • 416 Range Not Satisfiable if requested range end > file size #1588

  • Having a : or @ in a route does not work #1552

  • Added receive_timeout timeout for websocket to receive complete message. #1325

  • Added heartbeat parameter for websocket to automatically send ping message. #1024 #777

  • Remove web.Application dependency from web.UrlDispatcher #1510

  • Accepting back-pressure from slow websocket clients #1367

  • Do not pause transport during set_parser stage #1211

  • Lingering close doesn’t terminate before timeout #1559

  • setsockopt may raise OSError exception if socket is closed already #1595

  • Lots of CancelledError when requests are interrupted #1565

  • Allow users to specify what should happen to decoding errors when calling a responses text() method #1542

  • Back port std module http.cookies for python3.4.2 #1566

  • Maintain url’s fragment in client response #1314

  • Allow concurrently close WebSocket connection #754

  • Gzipped responses with empty body raises ContentEncodingError #609

  • Return 504 if request handle raises TimeoutError.

  • Refactor how we use keep-alive and close lingering timeouts.

  • Close response connection if we can not consume whole http message during client response release

  • Abort closed ssl client transports, broken servers can keep socket open un-limit time #1568

  • Log warning instead of RuntimeError is websocket connection is closed.

  • Deprecated: aiohttp.protocol.HttpPrefixParser will be removed in 1.4 #1590

  • Deprecated: Servers response’s .started, .start() and .can_start() method will be removed in 1.4 #1591

  • Deprecated: Adding sub app via app.router.add_subapp() is deprecated use app.add_subapp() instead, will be removed in 1.4 #1592

  • Deprecated: aiohttp.get(), aiohttp.options(), aiohttp.head(),, aiohttp.put(), aiohttp.patch(), aiohttp.delete(), and aiohttp.ws_connect() will be removed in 1.4 #1593

  • Deprecated: Application.finish() and Application.register_on_finish() will be removed in 1.4 #1602

Project details

Release history Release notifications | RSS feed

This version


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aiohttp-1.3.1.tar.gz (525.4 kB view hashes)

Uploaded Source

Built Distributions

aiohttp-1.3.1-cp36-cp36m-win_amd64.whl (150.0 kB view hashes)

Uploaded CPython 3.6m Windows x86-64

aiohttp-1.3.1-cp36-cp36m-win32.whl (148.8 kB view hashes)

Uploaded CPython 3.6m Windows x86

aiohttp-1.3.1-cp35-cp35m-win_amd64.whl (150.0 kB view hashes)

Uploaded CPython 3.5m Windows x86-64

aiohttp-1.3.1-cp35-cp35m-win32.whl (148.8 kB view hashes)

Uploaded CPython 3.5m Windows x86

aiohttp-1.3.1-cp35-cp35m-manylinux1_x86_64.whl (166.9 kB view hashes)

Uploaded CPython 3.5m

aiohttp-1.3.1-cp35-cp35m-manylinux1_i686.whl (164.2 kB view hashes)

Uploaded CPython 3.5m

aiohttp-1.3.1-cp34-cp34m-win_amd64.whl (147.9 kB view hashes)

Uploaded CPython 3.4m Windows x86-64

aiohttp-1.3.1-cp34-cp34m-win32.whl (147.6 kB view hashes)

Uploaded CPython 3.4m Windows x86

aiohttp-1.3.1-cp34-cp34m-manylinux1_x86_64.whl (167.0 kB view hashes)

Uploaded CPython 3.4m

aiohttp-1.3.1-cp34-cp34m-manylinux1_i686.whl (164.4 kB view hashes)

Uploaded CPython 3.4m

Supported by

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