Skip to main content

Async http client/server framework (asyncio)

Project description

Async http client/server framework

aiohttp logo

Travis status for master branch codecov.io status for master branch Latest PyPI package version Latest Read The Docs Chat on Gitter

Key Features

  • Supports both client and server side of HTTP protocol.
  • Supports both client and server Web-Sockets out-of-the-box without the Callback Hell.
  • 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):
    async with session.get(url) as response:
        return await response.text()

async def main():
    async with aiohttp.ClientSession() as session:
        html = await fetch(session, 'http://python.org')
        print(html)

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

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

    return ws


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

web.run_app(app)

Communication channels

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

Feel free to post your questions and ideas here.

gitter chat https://gitter.im/aio-libs/Lobby

We support Stack Overflow. Please add aiohttp tag to your question there.

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.

Keepsafe

The aiohttp community would like to thank Keepsafe (https://www.getkeepsafe.com) for it’s support in the early days of the project.

Source code

The latest developer version is available in a github repository: https://github.com/aio-libs/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

Changelog

3.2.0 (2018-05-06)

Features

  • Raise TooManyRedirects exception when client gets redirected too many times instead of returning last response. (#2631)
  • Extract route definitions into separate web_routedef.py file (#2876)
  • Raise an exception on request body reading after sending response. (#2895)
  • ClientResponse and RequestInfo now have real_url property, which is request url without fragment part being stripped (#2925)
  • Speed up connector limiting (#2937)
  • Added and links property for ClientResponse object (#2948)
  • Add request.config_dict for exposing nested applications data. (#2949)
  • Speed up HTTP headers serialization, server micro-benchmark runs 5% faster now. (#2957)
  • Apply assertions in debug mode only (#2966)

Bugfixes

  • expose property app for TestClient (#2891)
  • Call on_chunk_sent when write_eof takes as a param the last chunk (#2909)
  • A closing bracket was added to __repr__ of resources (#2935)
  • Fix compression of FileResponse (#2942)
  • Fixes some bugs in the limit connection feature (#2964)

Improved Documentation

  • Drop async_timeout usage from documentation for client API in favor of timeout parameter. (#2865)
  • Improve Gunicorn logging documentation (#2921)
  • Replace multipart writer .serialize() method with .write() in documentation. (#2965)

Deprecations and Removals

  • Deprecate Application.make_handler() (#2938)

Misc

  • #2958

Project details


Release history Release notifications

This version
History Node

3.2.0

Download files

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

Filename, size & hash SHA256 hash help File type Python version Upload date
aiohttp-3.2.0-cp35-cp35m-macosx_10_10_x86_64.whl (429.1 kB) Copy SHA256 hash SHA256 Wheel cp35
aiohttp-3.2.0-cp35-cp35m-macosx_10_11_x86_64.whl (427.4 kB) Copy SHA256 hash SHA256 Wheel cp35
aiohttp-3.2.0-cp35-cp35m-macosx_10_12_x86_64.whl (420.5 kB) Copy SHA256 hash SHA256 Wheel cp35
aiohttp-3.2.0-cp35-cp35m-manylinux1_i686.whl (720.0 kB) Copy SHA256 hash SHA256 Wheel cp35
aiohttp-3.2.0-cp35-cp35m-manylinux1_x86_64.whl (745.5 kB) Copy SHA256 hash SHA256 Wheel cp35
aiohttp-3.2.0-cp35-cp35m-win32.whl (403.9 kB) Copy SHA256 hash SHA256 Wheel cp35
aiohttp-3.2.0-cp35-cp35m-win_amd64.whl (417.6 kB) Copy SHA256 hash SHA256 Wheel cp35
aiohttp-3.2.0-cp36-cp36m-macosx_10_10_x86_64.whl (431.8 kB) Copy SHA256 hash SHA256 Wheel cp36
aiohttp-3.2.0-cp36-cp36m-macosx_10_11_x86_64.whl (429.8 kB) Copy SHA256 hash SHA256 Wheel cp36
aiohttp-3.2.0-cp36-cp36m-macosx_10_12_x86_64.whl (421.3 kB) Copy SHA256 hash SHA256 Wheel cp36
aiohttp-3.2.0-cp36-cp36m-manylinux1_i686.whl (735.5 kB) Copy SHA256 hash SHA256 Wheel cp36
aiohttp-3.2.0-cp36-cp36m-manylinux1_x86_64.whl (761.0 kB) Copy SHA256 hash SHA256 Wheel cp36
aiohttp-3.2.0-cp36-cp36m-win32.whl (405.5 kB) Copy SHA256 hash SHA256 Wheel cp36
aiohttp-3.2.0-cp36-cp36m-win_amd64.whl (419.5 kB) Copy SHA256 hash SHA256 Wheel cp36
aiohttp-3.2.0.tar.gz (719.7 kB) Copy SHA256 hash SHA256 Source None

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page