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.4.4 (2018-09-05)

  • Fix installation from sources when compiling toolkit is not available (#3241)

3.4.3 (2018-09-04)

  • Add app.pre_frozen state to properly handle startup signals in sub-applications. (#3237)

3.4.2 (2018-09-01)

  • Fix iter_chunks type annotation (#3230)

3.4.1 (2018-08-28)

  • Fix empty header parsing regression. (#3218)

  • Fix BaseRequest.raw_headers doc. (#3215)

  • Fix documentation building on ReadTheDocs (#3221)

3.4.0 (2018-08-25)

Features

  • Add type hints (#3049)

  • Add raise_for_status request parameter (#3073)

  • Add type hints to HTTP client (#3092)

  • Minor server optimizations (#3095)

  • Preserve the cause when HTTPException is raised from another exception. (#3096)

  • Add close_boundary option in MultipartWriter.write method. Support streaming (#3104)

  • Added a remove_slash option to the normalize_path_middleware factory. (#3173)

  • The class AbstractRouteDef is importable from aiohttp.web. (#3183)

Bugfixes

  • Prevent double closing when client connection is released before the last data_received() callback. (#3031)

  • Make redirect with normalize_path_middleware work when using url encoded paths. (#3051)

  • Postpone web task creation to connection establishment. (#3052)

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

  • fix UrlDispatcher has no attribute add_options, add web.options (#3062)

  • correct filename in content-disposition with multipart body (#3064)

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

  • raise 413 “Payload Too Large” rather than raising ValueError in request.post() Add helpful debug message to 413 responses (#3087)

  • Fix StreamResponse equality, now that they are MutableMapping objects. (#3100)

  • Fix server request objects comparison (#3116)

  • Do not hang on 206 Partial Content response with Content-Encoding: gzip (#3123)

  • Fix timeout precondition checkers (#3145)

Improved Documentation

  • Add a new FAQ entry that clarifies that you should not reuse response objects in middleware functions. (#3020)

  • Add FAQ section “Why is creating a ClientSession outside of an event loop dangerous?” (#3072)

  • Fix link to Rambler (#3115)

  • Fix TCPSite documentation on the Server Reference page. (#3146)

  • Fix documentation build configuration file for Windows. (#3147)

  • Remove no longer existing lingering_timeout parameter of Application.make_handler from documentation. (#3151)

  • Mention that app.make_handler is deprecated, recommend to use runners API instead. (#3157)

Deprecations and Removals

  • Drop loop.current_task() from helpers.current_task() (#2826)

  • Drop reader parameter from request.multipart(). (#3090)

Project details


Release history Release notifications | RSS feed

This version

3.4.4

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.4.tar.gz (822.1 kB view hashes)

Uploaded Source

Built Distributions

aiohttp-3.4.4-cp37-cp37m-win_amd64.whl (577.0 kB view hashes)

Uploaded CPython 3.7m Windows x86-64

aiohttp-3.4.4-cp37-cp37m-win32.whl (550.8 kB view hashes)

Uploaded CPython 3.7m Windows x86

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

Uploaded CPython 3.7m

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

Uploaded CPython 3.7m

aiohttp-3.4.4-cp37-cp37m-macosx_10_13_x86_64.whl (590.6 kB view hashes)

Uploaded CPython 3.7m macOS 10.13+ x86-64

aiohttp-3.4.4-cp37-cp37m-macosx_10_11_x86_64.whl (599.3 kB view hashes)

Uploaded CPython 3.7m macOS 10.11+ x86-64

aiohttp-3.4.4-cp37-cp37m-macosx_10_10_x86_64.whl (602.7 kB view hashes)

Uploaded CPython 3.7m macOS 10.10+ x86-64

aiohttp-3.4.4-cp36-cp36m-win_amd64.whl (577.0 kB view hashes)

Uploaded CPython 3.6m Windows x86-64

aiohttp-3.4.4-cp36-cp36m-win32.whl (551.2 kB view hashes)

Uploaded CPython 3.6m Windows x86

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

Uploaded CPython 3.6m

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

Uploaded CPython 3.6m

aiohttp-3.4.4-cp36-cp36m-macosx_10_13_x86_64.whl (594.1 kB view hashes)

Uploaded CPython 3.6m macOS 10.13+ x86-64

aiohttp-3.4.4-cp36-cp36m-macosx_10_11_x86_64.whl (599.4 kB view hashes)

Uploaded CPython 3.6m macOS 10.11+ x86-64

aiohttp-3.4.4-cp36-cp36m-macosx_10_10_x86_64.whl (602.6 kB view hashes)

Uploaded CPython 3.6m macOS 10.10+ x86-64

aiohttp-3.4.4-cp35-cp35m-win_amd64.whl (575.2 kB view hashes)

Uploaded CPython 3.5m Windows x86-64

aiohttp-3.4.4-cp35-cp35m-win32.whl (549.4 kB view hashes)

Uploaded CPython 3.5m Windows x86

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

Uploaded CPython 3.5m

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

Uploaded CPython 3.5m

aiohttp-3.4.4-cp35-cp35m-macosx_10_13_x86_64.whl (588.1 kB view hashes)

Uploaded CPython 3.5m macOS 10.13+ x86-64

aiohttp-3.4.4-cp35-cp35m-macosx_10_11_x86_64.whl (596.5 kB view hashes)

Uploaded CPython 3.5m macOS 10.11+ x86-64

aiohttp-3.4.4-cp35-cp35m-macosx_10_10_x86_64.whl (599.9 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