Skip to main content

Async http client/server framework (asyncio)

Project description

Async http client/server framework

aiohttp logo https://travis-ci.org/aio-libs/aiohttp.svg?branch=master https://codecov.io/gh/aio-libs/aiohttp/branch/master/graph/badge.svg https://badge.fury.io/py/aiohttp.svg

aiohttp 2.0 release!

For this release we completely refactored low-level implementation of http handling. Finally uvloop gives performance improvement. Overall performance improvement should be around 70-90% compared to 1.x version.

We took opportunity to refactor long standing api design problems across whole package. Client exceptions handling has been cleaned up and now much more straight forward. Client payload management simplified and allows to extend with any custom type. Client connection pool implementation has been redesigned as well, now there is no need for actively releasing response objects, aiohttp handles connection release automatically.

Another major change, we moved aiohttp development to public organization https://github.com/aio-libs

With this amount of api changes we had to make backward incompatible changes. Please check this migration document http://aiohttp.readthedocs.io/en/latest/migration.html

Please report problems or annoyance with with api to https://github.com/aio-libs/aiohttp

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:
            await ws.send_str("Hello, {}".format(msg.data))
        elif msg.type == web.MsgType.binary:
            await 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.

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

Changes

2.2.4 (2017-08-02)

  • Fix issue with synchronous session closing when using ClientSession as an asynchronous context manager. (#2063)

2.2.3 (2017-07-04)

  • Fix _CoroGuard for python 3.4

2.2.2 (2017-07-03)

  • Allow await session.close() along with yield from session.close()

2.2.1 (2017-07-02)

  • Relax yarl requirement to 0.11+

  • Backport #2026: session.close is a coroutine (#2029)

2.2.0 (2017-06-20)

  • Add doc for add_head, update doc for add_get. #1944

  • Fixed consecutive calls for Response.write_eof.

  • Retain method attributes (e.g. __doc__) when registering synchronous handlers for resources. #1953

  • Added signal TERM handling in run_app to gracefully exit #1932

  • Fix websocket issues caused by frame fragmentation. #1962

  • Raise RuntimeError is you try to set the Content Length and enable chunked encoding at the same time #1941

  • Small update for unittest_run_loop

  • Use CIMultiDict for ClientRequest.skip_auto_headers #1970

  • Fix wrong startup sequence: test server and run_app() are not raise DeprecationWarning now #1947

  • Make sure cleanup signal is sent if startup signal has been sent #1959

  • Fixed server keep-alive handler, could cause 100% cpu utilization #1955

  • Connection can be destroyed before response get processed if await aiohttp.request(..) is used #1981

  • MultipartReader does not work with -OO #1969

  • Fixed ClientPayloadError with blank Content-Encoding header #1931

  • Support deflate encoding implemented in httpbin.org/deflate #1918

  • Fix BadStatusLine caused by extra CRLF after POST data #1792

  • Keep a reference to ClientSession in response object #1985

  • Deprecate undocumented app.on_loop_available signal #1978

2.1.0 (2017-05-26)

  • Added support for experimental async-tokio event loop written in Rust https://github.com/PyO3/tokio

  • Write to transport \r\n before closing after keepalive timeout, otherwise client can not detect socket disconnection. #1883

  • Only call loop.close in run_app if the user did not supply a loop. Useful for allowing clients to specify their own cleanup before closing the asyncio loop if they wish to tightly control loop behavior

  • Content disposition with semicolon in filename #917

  • Added request_info to response object and ClientResponseError. #1733

  • Added history to ClientResponseError. #1741

  • Allow to disable redirect url re-quoting #1474

  • Handle RuntimeError from transport #1790

  • Dropped “%O” in access logger #1673

  • Added args and kwargs to unittest_run_loop. Useful with other decorators, for example @patch. #1803

  • Added iter_chunks to response.content object. #1805

  • Avoid creating TimerContext when there is no timeout to allow compatibility with Tornado. #1817 #1180

  • Add proxy_from_env to ClientRequest to read from environment variables. #1791

  • Add DummyCookieJar helper. #1830

  • Fix assertion errors in Python 3.4 from noop helper. #1847

  • Do not unquote + in match_info values #1816

  • Use Forwarded, X-Forwarded-Scheme and X-Forwarded-Host for better scheme and host resolution. #1134

  • Fix sub-application middlewares resolution order #1853

  • Fix applications comparison #1866

  • Fix static location in index when prefix is used #1662

  • Make test server more reliable #1896

  • Extend list of web exceptions, add HTTPUnprocessableEntity, HTTPFailedDependency, HTTPInsufficientStorage status codes #1920

2.0.7 (2017-04-12)

  • Fix pypi distribution

  • Fix exception description #1807

  • Handle socket error in FileResponse #1773

  • Cancel websocket heartbeat on close #1793

2.0.6 (2017-04-04)

  • Keeping blank values for request.post() and multipart.form() #1765

  • TypeError in data_received of ResponseHandler #1770

  • Fix web.run_app not to bind to default host-port pair if only socket is passed #1786

2.0.5 (2017-03-29)

  • Memory leak with aiohttp.request #1756

  • Disable cleanup closed ssl transports by default.

  • Exception in request handling if the server responds before the body is sent #1761

2.0.4 (2017-03-27)

  • Memory leak with aiohttp.request #1756

  • Encoding is always UTF-8 in POST data #1750

  • Do not add “Content-Disposition” header by default #1755

2.0.3 (2017-03-24)

  • Call https website through proxy will cause error #1745

  • Fix exception on multipart/form-data post if content-type is not set #1743

2.0.2 (2017-03-21)

  • Fixed Application.on_loop_available signal #1739

  • Remove debug code

2.0.1 (2017-03-21)

  • Fix allow-head to include name on route #1737

  • Fixed AttributeError in WebSocketResponse.can_prepare #1736

2.0.0 (2017-03-20)

  • Added json to ClientSession.request() method #1726

  • Added session’s raise_for_status parameter, automatically calls raise_for_status() on any request. #1724

  • response.json() raises ClientReponseError exception if response’s content type does not match #1723

    • Cleanup timer and loop handle on any client exception.

  • Deprecate loop parameter for Application’s constructor

2.0.0rc1 (2017-03-15)

  • Properly handle payload errors #1710

  • Added ClientWebSocketResponse.get_extra_info() #1717

  • It is not possible to combine Transfer-Encoding and chunked parameter, same for compress and Content-Encoding #1655

  • Connector’s limit parameter indicates total concurrent connections. New limit_per_host added, indicates total connections per endpoint. #1601

  • Use url’s raw_host for name resolution #1685

  • Change ClientResponse.url to yarl.URL instance #1654

  • Add max_size parameter to web.Request reading methods #1133

  • Web Request.post() stores data in temp files #1469

  • Add the allow_head=True keyword argument for add_get #1618

  • run_app and the Command Line Interface now support serving over Unix domain sockets for faster inter-process communication.

  • run_app now supports passing a preexisting socket object. This can be useful e.g. for socket-based activated applications, when binding of a socket is done by the parent process.

  • Implementation for Trailer headers parser is broken #1619

  • Fix FileResponse to not fall on bad request (range out of file size)

  • Fix FileResponse to correct stream video to Chromes

  • Deprecate public low-level api #1657

  • Deprecate encoding parameter for ClientSession.request() method

  • Dropped aiohttp.wsgi #1108

  • Dropped version from ClientSession.request() method

  • Dropped websocket version 76 support #1160

  • Dropped: aiohttp.protocol.HttpPrefixParser #1590

  • Dropped: Servers response’s .started, .start() and .can_start() method #1591

  • Dropped: Adding sub app via app.router.add_subapp() is deprecated use app.add_subapp() instead #1592

  • Dropped: Application.finish() and Application.register_on_finish() #1602

  • Dropped: web.Request.GET and web.Request.POST

  • Dropped: aiohttp.get(), aiohttp.options(), aiohttp.head(), aiohttp.post(), aiohttp.put(), aiohttp.patch(), aiohttp.delete(), and aiohttp.ws_connect() #1593

  • Dropped: aiohttp.web.WebSocketResponse.receive_msg() #1605

  • Dropped: ServerHttpProtocol.keep_alive_timeout attribute and keep-alive, keep_alive_on, timeout, log constructor parameters #1606

  • Dropped: TCPConnector’s` .resolve, .resolved_hosts, .clear_resolved_hosts() attributes and resolve constructor parameter #1607

  • Dropped ProxyConnector #1609

Project details


Release history Release notifications | RSS feed

This version

2.2.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-2.2.4.tar.gz (788.5 kB view details)

Uploaded Source

Built Distributions

aiohttp-2.2.4-cp36-cp36m-win_amd64.whl (320.1 kB view details)

Uploaded CPython 3.6m Windows x86-64

aiohttp-2.2.4-cp36-cp36m-win32.whl (311.3 kB view details)

Uploaded CPython 3.6m Windows x86

aiohttp-2.2.4-cp36-cp36m-manylinux1_x86_64.whl (770.7 kB view details)

Uploaded CPython 3.6m

aiohttp-2.2.4-cp36-cp36m-manylinux1_i686.whl (749.1 kB view details)

Uploaded CPython 3.6m

aiohttp-2.2.4-cp35-cp35m-win_amd64.whl (318.5 kB view details)

Uploaded CPython 3.5m Windows x86-64

aiohttp-2.2.4-cp35-cp35m-win32.whl (309.8 kB view details)

Uploaded CPython 3.5m Windows x86

aiohttp-2.2.4-cp35-cp35m-manylinux1_x86_64.whl (759.0 kB view details)

Uploaded CPython 3.5m

aiohttp-2.2.4-cp35-cp35m-manylinux1_i686.whl (735.9 kB view details)

Uploaded CPython 3.5m

aiohttp-2.2.4-cp34-cp34m-win_amd64.whl (314.0 kB view details)

Uploaded CPython 3.4m Windows x86-64

aiohttp-2.2.4-cp34-cp34m-win32.whl (308.7 kB view details)

Uploaded CPython 3.4m Windows x86

aiohttp-2.2.4-cp34-cp34m-manylinux1_x86_64.whl (559.5 kB view details)

Uploaded CPython 3.4m

aiohttp-2.2.4-cp34-cp34m-manylinux1_i686.whl (535.6 kB view details)

Uploaded CPython 3.4m

File details

Details for the file aiohttp-2.2.4.tar.gz.

File metadata

  • Download URL: aiohttp-2.2.4.tar.gz
  • Upload date:
  • Size: 788.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for aiohttp-2.2.4.tar.gz
Algorithm Hash digest
SHA256 8e72be7111c487e5a474d8d8020e29be5de259bb7a72db1ec74af37ba7aa664a
MD5 897e6de4704b1736213c2da069850f3a
BLAKE2b-256 f35d50e45c48f69f80728704689fe4fd58a9cff97a26a2c9451af01510116b8b

See more details on using hashes here.

File details

Details for the file aiohttp-2.2.4-cp36-cp36m-win_amd64.whl.

File metadata

File hashes

Hashes for aiohttp-2.2.4-cp36-cp36m-win_amd64.whl
Algorithm Hash digest
SHA256 6fefc75ac82747f5e0c0d38e7301997d2aa35de3f5d7a600031e5e632a4d5197
MD5 4d180a4e7200b7e17134f6080063f4c7
BLAKE2b-256 bd1bc42f332dc3b235a8f37c182404041c6dcd67ab6e18b66a9d9bbebf8120f0

See more details on using hashes here.

File details

Details for the file aiohttp-2.2.4-cp36-cp36m-win32.whl.

File metadata

File hashes

Hashes for aiohttp-2.2.4-cp36-cp36m-win32.whl
Algorithm Hash digest
SHA256 60ad89a2269cf231adfd911546003fea2e627e5f2641d1b1e72ff74b3e38a1a4
MD5 b2e1c338076f677b1247bc9c19ff2f2c
BLAKE2b-256 6bad3965f81e6b9f760291a405d4192c8eadb79d78995a10ab23690b7419cdc8

See more details on using hashes here.

File details

Details for the file aiohttp-2.2.4-cp36-cp36m-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for aiohttp-2.2.4-cp36-cp36m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 9b146fbeab472deaa5e586977241812b6d57a3f608c05576910c5c8cf0218353
MD5 869f11d534a27e9cb0ed6a1f528de6e1
BLAKE2b-256 d52ba99aacf3826ab276a45dc08554dfc6edb38c8d8d727b5964d28ef890c6f4

See more details on using hashes here.

File details

Details for the file aiohttp-2.2.4-cp36-cp36m-manylinux1_i686.whl.

File metadata

File hashes

Hashes for aiohttp-2.2.4-cp36-cp36m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 0c149594046854346d460ee7b35f24e4f157bd4debd59ffbd2aad468d7a9b878
MD5 64d155cae49bb8f00851d6f96a4a59fb
BLAKE2b-256 100d21292e262d41539c1bf4bb4b44f1d4fd2cd4a422e7470677d9f82b936900

See more details on using hashes here.

File details

Details for the file aiohttp-2.2.4-cp35-cp35m-win_amd64.whl.

File metadata

File hashes

Hashes for aiohttp-2.2.4-cp35-cp35m-win_amd64.whl
Algorithm Hash digest
SHA256 6c895352171fec76eda7e77a9389789214c83d0418bcb657b51a9538822db65b
MD5 79c882e3d4cdaeca7d2ac4d4bd2247fc
BLAKE2b-256 3e0916dfa2ec77662292f813f33babc7b6a3b8ff4d506ba3af66effc1a4796cc

See more details on using hashes here.

File details

Details for the file aiohttp-2.2.4-cp35-cp35m-win32.whl.

File metadata

File hashes

Hashes for aiohttp-2.2.4-cp35-cp35m-win32.whl
Algorithm Hash digest
SHA256 fe780cb75d598bd6fe5e7c704b89be6f27f420c5589dece64d8833ee461cff92
MD5 9a2d9447ee983b6b128a8d0fcd95b440
BLAKE2b-256 7b1af64d5a09131b569e32eff33f01f1b0f4fe5ae320487b7adac59fd8e559b0

See more details on using hashes here.

File details

Details for the file aiohttp-2.2.4-cp35-cp35m-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for aiohttp-2.2.4-cp35-cp35m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 65990e3013d9d2f30dbd96cd05d82b02002006a733a24fd70fd9f81d507ad102
MD5 6d7bb50e588a88d45373637ea5b9386e
BLAKE2b-256 c94d690ec8660a0b089b4f8494caa085aa711367bb63d0d12a0d42b9429f4e32

See more details on using hashes here.

File details

Details for the file aiohttp-2.2.4-cp35-cp35m-manylinux1_i686.whl.

File metadata

File hashes

Hashes for aiohttp-2.2.4-cp35-cp35m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 796dad34d0ae75f6b68f71db13d4f1cd85dd393d135502ce4f3f57254480299e
MD5 654a83062aa28ea77bae1ee954db04b0
BLAKE2b-256 5d4c3a84e99fc43813f61ddb1418c6188b0e366d91624d1dd03c10c1ae098d66

See more details on using hashes here.

File details

Details for the file aiohttp-2.2.4-cp34-cp34m-win_amd64.whl.

File metadata

File hashes

Hashes for aiohttp-2.2.4-cp34-cp34m-win_amd64.whl
Algorithm Hash digest
SHA256 b4c35fee746fc01283c5f2f5faf661a26898c5927d5179c247137259f2ee5f4a
MD5 8f8f090ff985ce02176eb904108555a3
BLAKE2b-256 780a42f677ca34f2b3ab606705c99c352751f78665d6855370f528d675a1376e

See more details on using hashes here.

File details

Details for the file aiohttp-2.2.4-cp34-cp34m-win32.whl.

File metadata

File hashes

Hashes for aiohttp-2.2.4-cp34-cp34m-win32.whl
Algorithm Hash digest
SHA256 6bdfcd134275e48dde374af8a1b3d130c42eaa743f5eab952b8752d1ca6d4407
MD5 338a492083a7f35055b0754ed21821ed
BLAKE2b-256 2476f9aded6da0fd58c0a2fe3811760c655082d9e639990c85ca641e75136cbe

See more details on using hashes here.

File details

Details for the file aiohttp-2.2.4-cp34-cp34m-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for aiohttp-2.2.4-cp34-cp34m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 44df3197cbdd3fd953a2e76d1f354bea91ca274fe8246afb89fe8ac364df4b9f
MD5 01cfce17261673295f7594ad8abbd804
BLAKE2b-256 f11a77a25bd39313dcb6694548533fc9a98972ac8817a75dba9f3c8ede4ebebb

See more details on using hashes here.

File details

Details for the file aiohttp-2.2.4-cp34-cp34m-manylinux1_i686.whl.

File metadata

File hashes

Hashes for aiohttp-2.2.4-cp34-cp34m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 10a59c3e2ad7e18531edfbbab51e1c0432c108348bc3ac2a703be09ca3914eb3
MD5 584914c3b983cc21accdcba5e4d8ec09
BLAKE2b-256 9576d24ddad0cc4514d30a707fc1bce2babcf7c405f8c19f614a92b855845f18

See more details on using hashes here.

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