http client/server for asyncio (PEP-3156)
Python HTML Makefile Shell Batchfile PLpgSQL
Latest commit fa3b1ff Dec 24, 2016 @o3o3o o3o3o committed with asvetlov add numprocs in supervisord (#1502)
add numprocs in supervisord
Permalink
Failed to load latest commit information.
aiohttp Fix StreamResponse output_length typo. (#1490) Dec 18, 2016
benchmark Update twisted from 16.5.0 to 16.6.0 Nov 25, 2016
demos Drop pytest-asyncio from dependencies Dec 19, 2016
docs add numprocs in supervisord (#1502) Dec 24, 2016
examples Fix examples Dec 16, 2016
tests Fix StreamResponse output_length typo. (#1490) Dec 18, 2016
.coveragerc Improve test coverage Jan 14, 2015
.gitignore Optimize flake run Aug 23, 2016
.travis.yml Remove MacOSX again Nov 10, 2016
CHANGES.rst Fix CHANGES Dec 18, 2016
CONTRIBUTING.rst Fixed stream reading method of BodyPartReader Nov 27, 2016
CONTRIBUTORS.txt fix polls demo run application (#1487) Dec 18, 2016
HISTORY.rst Bump to 1.2.0 Dec 17, 2016
ISSUE_TEMPLATE.md some corrections and clarifications in the issue/pr templates. (#911) Jun 4, 2016
LICENSE.txt Update LICENSE's years Jan 3, 2016
MANIFEST.in fix MANIFEST.in: CHANGES.{txt,rst} Aug 12, 2016
Makefile New server (#1362) Nov 9, 2016
PULL_REQUEST_TEMPLATE.md Fixed PR template (#1328) Oct 23, 2016
README.rst Fix typo in README Oct 29, 2016
appveyor.yml Upgrade appveyor version number Oct 19, 2016
build-wheels.sh Update wheel building instructions Jul 14, 2016
build.cmd Try to fix 64 build on Windows Jan 8, 2016
codecov.yml Shrink codecov range Aug 27, 2016
http2.txt Add http2 notes Sep 16, 2016
requirements-ci.txt Update sphinx from 1.5 to 1.5.1 (#1481) Dec 14, 2016
requirements-dev.txt Pin pytest-sugar to latest version 0.7.1 (#1276) Sep 30, 2016
requirements-wheel.txt Update cython to 0.25.2 (#1465) Dec 9, 2016
run_docker.sh Update wheel building instructions Jul 14, 2016
setup.cfg Increate timeout for tests Nov 16, 2016
setup.py Update dependecies versions Dec 4, 2016
tox.ini Add pyflakes version requirements Oct 6, 2015

README.rst

http client/server for asyncio

aiohttp logo

https://travis-ci.org/KeepSafe/aiohttp.svg?branch=master

Features

  • 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

Client

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, 'http://python.org')
        print(html)

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

Server

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(msg.data))
        elif msg.type == web.MsgType.binary:
            ws.send_bytes(msg.data)
        elif msg.type == web.MsgType.close:
            break

    return ws


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

web.run_app(app)

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:

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

Documentation

https://aiohttp.readthedocs.io/

Discussion list

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

Requirements

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

License

aiohttp is offered under the Apache 2 license.

Source code

The latest developer version is available in a github repository: https://github.com/KeepSafe/aiohttp

Benchmarks

If you are interested in by efficiency, AsyncIO community maintains a list of benchmarks on the official wiki: https://github.com/python/asyncio/wiki/Benchmarks