Skip to main content

Async http client/server framework (asyncio)

Project description

Async http client/server framework

aiohttp logo

Travis status for master branch AppVeyor 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)

Documentation

https://aiohttp.readthedocs.io/

Demos

https://github.com/aio-libs/aiohttp-demos

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.3.2 (2018-06-12)

  • Many HTTP proxies has buggy keepalive support. Let’s not reuse connection but close it after processing every response. (#3070)

  • Provide vendor source files in tarball (#3076)

3.3.1 (2018-06-05)

  • Fix sock_read timeout. (#3053)

  • When using a server-request body as the data= argument of a client request, iterate over the content with readany instead of readline to avoid Line too long errors. (#3054)

3.3.0 (2018-06-01)

Features

  • Raise ConnectionResetError instead of CancelledError on trying to write to a closed stream. (#2499)

  • Implement ClientTimeout class and support socket read timeout. (#2768)

  • Enable logging when aiohttp.web is used as a program (#2956)

  • Add canonical property to resources (#2968)

  • Forbid reading response BODY after release (#2983)

  • Implement base protocol class to avoid a dependency from internal asyncio.streams.FlowControlMixin (#2986)

  • Cythonize @helpers.reify, 5% boost on macro benchmark (#2995)

  • Optimize HTTP parser (#3015)

  • Implement runner.addresses property. (#3036)

  • Use bytearray instead of a list of bytes in websocket reader. It improves websocket message reading a little. (#3039)

  • Remove heartbeat on closing connection on keepalive timeout. The used hack violates HTTP protocol. (#3041)

  • Limit websocket message size on reading to 4 MB by default. (#3045)

Bugfixes

  • Don’t reuse a connection with the same URL but different proxy/TLS settings (#2981)

  • When parsing the Forwarded header, the optional port number is now preserved. (#3009)

Improved Documentation

  • Make Change Log more visible in docs (#3029)

  • Make style and grammar improvements on the FAQ page. (#3030)

  • Document that signal handlers should be async functions since aiohttp 3.0 (#3032)

Deprecations and Removals

  • Deprecate custom application’s router. (#3021)

Misc

  • #3008, #3011

Release history Release notifications | RSS feed

Download files

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

Source Distribution

aiohttp-3.4.0b2.tar.gz (822.6 kB view hashes)

Uploaded Source

Built Distributions

aiohttp-3.4.0b2-cp37-cp37m-win_amd64.whl (576.3 kB view hashes)

Uploaded CPython 3.7m Windows x86-64

aiohttp-3.4.0b2-cp37-cp37m-win32.whl (550.1 kB view hashes)

Uploaded CPython 3.7m Windows x86

aiohttp-3.4.0b2-cp37-cp37m-manylinux1_x86_64.whl (1.1 MB view hashes)

Uploaded CPython 3.7m

aiohttp-3.4.0b2-cp37-cp37m-manylinux1_i686.whl (1.1 MB view hashes)

Uploaded CPython 3.7m

aiohttp-3.4.0b2-cp37-cp37m-macosx_10_13_x86_64.whl (590.0 kB view hashes)

Uploaded CPython 3.7m macOS 10.13+ x86-64

aiohttp-3.4.0b2-cp37-cp37m-macosx_10_11_x86_64.whl (598.5 kB view hashes)

Uploaded CPython 3.7m macOS 10.11+ x86-64

aiohttp-3.4.0b2-cp37-cp37m-macosx_10_10_x86_64.whl (602.0 kB view hashes)

Uploaded CPython 3.7m macOS 10.10+ x86-64

aiohttp-3.4.0b2-cp36-cp36m-win_amd64.whl (576.3 kB view hashes)

Uploaded CPython 3.6m Windows x86-64

aiohttp-3.4.0b2-cp36-cp36m-win32.whl (550.5 kB view hashes)

Uploaded CPython 3.6m Windows x86

aiohttp-3.4.0b2-cp36-cp36m-manylinux1_x86_64.whl (1.1 MB view hashes)

Uploaded CPython 3.6m

aiohttp-3.4.0b2-cp36-cp36m-manylinux1_i686.whl (1.1 MB view hashes)

Uploaded CPython 3.6m

aiohttp-3.4.0b2-cp36-cp36m-macosx_10_13_x86_64.whl (593.4 kB view hashes)

Uploaded CPython 3.6m macOS 10.13+ x86-64

aiohttp-3.4.0b2-cp36-cp36m-macosx_10_11_x86_64.whl (598.6 kB view hashes)

Uploaded CPython 3.6m macOS 10.11+ x86-64

aiohttp-3.4.0b2-cp36-cp36m-macosx_10_10_x86_64.whl (601.9 kB view hashes)

Uploaded CPython 3.6m macOS 10.10+ x86-64

aiohttp-3.4.0b2-cp35-cp35m-win_amd64.whl (574.4 kB view hashes)

Uploaded CPython 3.5m Windows x86-64

aiohttp-3.4.0b2-cp35-cp35m-win32.whl (548.7 kB view hashes)

Uploaded CPython 3.5m Windows x86

aiohttp-3.4.0b2-cp35-cp35m-manylinux1_x86_64.whl (1.1 MB view hashes)

Uploaded CPython 3.5m

aiohttp-3.4.0b2-cp35-cp35m-manylinux1_i686.whl (1.1 MB view hashes)

Uploaded CPython 3.5m

aiohttp-3.4.0b2-cp35-cp35m-macosx_10_13_x86_64.whl (587.4 kB view hashes)

Uploaded CPython 3.5m macOS 10.13+ x86-64

aiohttp-3.4.0b2-cp35-cp35m-macosx_10_11_x86_64.whl (595.8 kB view hashes)

Uploaded CPython 3.5m macOS 10.11+ x86-64

aiohttp-3.4.0b2-cp35-cp35m-macosx_10_10_x86_64.whl (599.2 kB view hashes)

Uploaded CPython 3.5m macOS 10.10+ x86-64

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