Skip to main content
This is a pre-production deployment of Warehouse. Changes made here affect the production instance of PyPI (pypi.python.org).
Help us improve Python packaging - Donate today!

http client/server for asyncio

Project Description

http client/server for asyncio

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.

Requirements

Optionally you may install cChardet library: https://pypi.python.org/pypi/cchardet/1.0.0

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

Getting started

Client

To retrieve something from the web:

import aiohttp
import asyncio

async def get_body(client, url):
    async with client.get(url) as response:
        return await response.read()

if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    client = aiohttp.ClientSession(loop=loop)
    raw_html = loop.run_until_complete(get_body(client, 'http://python.org'))
    print(raw_html)
    client.close()

If you want to use timeouts for aiohttp client please use standard asyncio approach:

yield from asyncio.wait_for(client.get(url), 10)

Server

This is simple usage example:

import asyncio
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'))

async def wshandler(request):
    ws = web.WebSocketResponse()
    ws.start(request)

    while True:
        msg = await ws.receive()

        if msg.tp == web.MsgType.text:
            ws.send_str("Hello, {}".format(msg.data))
        elif msg.tp == web.MsgType.binary:
            ws.send_bytes(msg.data)
        elif msg.tp == web.MsgType.close:
            break

    return ws


async def init(loop):
    app = web.Application(loop=loop)
    app.router.add_route('GET', '/echo', wshandler)
    app.router.add_route('GET', '/{name}', handle)

    srv = await loop.create_server(app.make_handler(),
                                        '127.0.0.1', 8080)
    print("Server started at http://127.0.0.1:8080")
    return srv

loop = asyncio.get_event_loop()
loop.run_until_complete(init(loop))
loop.run_forever()

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()

shoud be replaced by:

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

CHANGES

0.18.1 (20-10-2015)

  • Relax rule for router names: they may contain dots and columns starting from now

0.18.0 (19-10-2015)

  • Use errors.HttpProcessingError.message as HTTP error reason and message #459

  • Optimize cythonized multidict a bit

  • Change repr’s of multidicts and multidict views

  • default headers in ClientSession are now case-insensitive

  • Make ‘=’ char and ‘wss://’ schema safe in urls #477

  • ClientResponse.close() forces connection closing by default from now #479 N.B. Backward incompatible change: was .close(force=False) Using `force parameter for the method is deprecated: use .release() instead.

  • Properly requote URL’s path #480

  • add skip_auto_headers parameter for client API #486

  • Properly parse URL path in aiohttp.web.Request #489

  • Raise RuntimeError when chunked enabled and HTTP is 1.0 #488

  • Fix a bug with processing io.BytesIO as data parameter for client API #500

  • Skip auto-generation of Content-Type header #507

  • Use sendfile facility for static file handling #503

  • Default response_factory in app.router.add_static now is StreamResponse, not None. The functionality is not changed if default is not specified.

  • Drop ClientResponse.message attribute, it was always implementation detail.

  • Streams are optimized for speed and mostly memory in case of a big HTTP message sizes #496

  • Fix a bug for server-side cookies for dropping cookie and setting it again without Max-Age parameter.

  • Don’t trim redirect URL in client API #499

  • Extend precision of access log “D” to milliseconds #527

  • Deprecate StreamResponse.start() method in favor of StreamResponse.prepare() coroutine #525

    .start() is still supported but responses begun with .start() doesn’t call signal for response preparing to be sent.

  • Add StreamReader.__repr__

  • Drop Python 3.3 support, from now minimal required version is Python 3.4.1 #541

  • Add async with support for ClientSession.request() and family #536

  • Ignore message body on 204 and 304 responses #505

  • TCPConnector processed both IPv4 and IPv6 by default #559

  • Add .routes() view for urldispatcher #519

  • Route name should be a valid identifier name from now #567

  • Implement server signals #562

  • Drop an year-old deprecated files parameter from client API.

  • Added async for support for aiohttp stream #542

Release History

Release History

History Node

2.3.9

History Node

2.3.8

History Node

2.3.7

History Node

2.3.6

History Node

2.3.5

History Node

2.3.4

History Node

2.3.3

History Node

2.3.2

History Node

2.3.2b3

History Node

2.3.2b2

History Node

2.3.1

History Node

2.3.1a1

History Node

2.3.0

History Node

2.3.0a4

History Node

2.3.0a3

History Node

2.3.0a2

History Node

2.3.0a1

History Node

2.2.5

History Node

2.2.4

History Node

2.2.3

History Node

2.2.2

History Node

2.2.1

History Node

2.2.0

History Node

2.1.0

History Node

2.0.7

History Node

2.0.6

History Node

2.0.5

History Node

2.0.4

History Node

2.0.3

History Node

2.0.2

History Node

2.0.1

History Node

2.0.0

History Node

2.0.0rc1

History Node

1.3.5

History Node

1.3.4

History Node

1.3.3

History Node

1.3.2

History Node

1.3.1

History Node

1.3.0

History Node

1.2.0

History Node

1.1.6

History Node

1.1.5

History Node

1.1.4

History Node

1.1.3

History Node

1.1.2

History Node

1.1.1

History Node

1.1.0

History Node

1.0.5

History Node

1.0.3

History Node

1.0.2

History Node

1.0.1

History Node

1.0.0

History Node

0.22.5

History Node

0.22.4

History Node

0.22.3

History Node

0.22.2

History Node

0.22.1

History Node

0.22.0

History Node

0.22.0b6

History Node

0.22.0b5

History Node

0.22.0b4

History Node

0.22.0b3

History Node

0.22.0b2

History Node

0.22.0b1

History Node

0.22.0b0

History Node

0.22.0a0

History Node

0.21.6

History Node

0.21.5

History Node

0.21.4

History Node

0.21.2

History Node

0.21.1

History Node

0.21.0

History Node

0.20.2

History Node

0.20.1

History Node

0.20.0

History Node

0.19.0

History Node

0.18.4

History Node

0.18.3

History Node

0.18.2

This version
History Node

0.18.1

History Node

0.18.0

History Node

0.17.4

History Node

0.17.3

History Node

0.17.2

History Node

0.17.1

History Node

0.17.0

History Node

0.16.6

History Node

0.16.5

History Node

0.16.4

History Node

0.16.3

History Node

0.16.2

History Node

0.16.1

History Node

0.16.0

History Node

0.15.3

History Node

0.15.2

History Node

0.15.1

History Node

0.15.0

History Node

0.14.4

History Node

0.14.3

History Node

0.14.2

History Node

0.14.1

History Node

0.14.0

History Node

0.13.1

History Node

0.13.0

History Node

0.12.0

History Node

0.11.0

History Node

0.10.2

History Node

0.10.1

History Node

0.10.0

History Node

0.9.3

History Node

0.9.2

History Node

0.9.1

History Node

0.9.0

History Node

0.8.4

History Node

0.8.3

History Node

0.8.2

History Node

0.8.1

History Node

0.8.0

History Node

0.7.3

History Node

0.7.2

History Node

0.7.1

History Node

0.7.0

History Node

0.6.5

History Node

0.6.4

History Node

0.6.3

History Node

0.6.2

History Node

0.6.1

History Node

0.6.0

History Node

0.5.0

History Node

0.4.4

History Node

0.4.3

History Node

0.4.2

History Node

0.4.1

History Node

0.4

History Node

0.3

History Node

0.2

History Node

0.1

Download Files

Download Files

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

File Name & Checksum SHA256 Checksum Help Version File Type Upload Date
aiohttp-0.18.1.tar.gz (2.2 MB) Copy SHA256 Checksum SHA256 Source Oct 20, 2015

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting