aiohttp

HTTP client/server for asyncio (PEP 3156).

Features

Library Installation

$ pip install aiohttp

You may want to install optional cchardet library as faster replacement for chardet:

$ pip install cchardet

Getting Started

Client example:

import asyncio
import aiohttp

async def fetch_page(session, url):
    with aiohttp.Timeout(10):
        async with session.get(url) as response:
            assert response.status == 200
            return await response.read()

loop = asyncio.get_event_loop()
with aiohttp.ClientSession(loop=loop) as session:
    content = loop.run_until_complete(
        fetch_page(session, 'http://python.org'))
    print(content)

Server example:

from aiohttp import web

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

app = web.Application()
app.router.add_route('GET', '/{name}', handle)

web.run_app(app)

Note

Throughout this documentation, examples utilize the async/await syntax introduced by PEP 492 that is only valid for Python 3.5+.

If you are using Python 3.4, please replace await with yield from and async def with a @coroutine decorator. For example, this:

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

should be replaced by:

@asyncio.coroutine
def coro(...):
    ret = yield from f()

Tutorial

Polls tutorial

Source code

The project is hosted on GitHub

Please feel free to file an issue on the bug tracker if you have found a bug or have some suggestion in order to improve the library.

The library uses Travis for Continuous Integration.

Dependencies

  • Python Python 3.4.1+

  • chardet library

  • Optional cchardet library as faster replacement for chardet.

    Install it explicitly via:

    $ pip install cchardet
    

Discussion list

aio-libs google group: https://groups.google.com/forum/#!forum/aio-libs

Feel free to post your questions and ideas here.

Contributing

Please read the instructions for contributors before making a Pull Request.

Authors and License

The aiohttp package is written mostly by Nikolay Kim and Andrew Svetlov.

It’s Apache 2 licensed and freely available.

Feel free to improve this package and send a pull request to GitHub.

Contents

Indices and tables

blog comments powered by Disqus