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.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

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

  • 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

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.1.0.tar.gz (748.6 kB view details)

Uploaded Source

Built Distributions

aiohttp-2.1.0-cp36-cp36m-win_amd64.whl (268.5 kB view details)

Uploaded CPython 3.6m Windows x86-64

aiohttp-2.1.0-cp36-cp36m-win32.whl (262.4 kB view details)

Uploaded CPython 3.6m Windows x86

aiohttp-2.1.0-cp36-cp36m-manylinux1_x86_64.whl (661.5 kB view details)

Uploaded CPython 3.6m

aiohttp-2.1.0-cp36-cp36m-manylinux1_i686.whl (644.2 kB view details)

Uploaded CPython 3.6m

aiohttp-2.1.0-cp35-cp35m-win_amd64.whl (267.0 kB view details)

Uploaded CPython 3.5m Windows x86-64

aiohttp-2.1.0-cp35-cp35m-win32.whl (261.1 kB view details)

Uploaded CPython 3.5m Windows x86

aiohttp-2.1.0-cp35-cp35m-manylinux1_x86_64.whl (652.6 kB view details)

Uploaded CPython 3.5m

aiohttp-2.1.0-cp35-cp35m-manylinux1_i686.whl (635.6 kB view details)

Uploaded CPython 3.5m

aiohttp-2.1.0-cp34-cp34m-win_amd64.whl (264.2 kB view details)

Uploaded CPython 3.4m Windows x86-64

aiohttp-2.1.0-cp34-cp34m-win32.whl (260.5 kB view details)

Uploaded CPython 3.4m Windows x86

aiohttp-2.1.0-cp34-cp34m-manylinux1_x86_64.whl (451.1 kB view details)

Uploaded CPython 3.4m

aiohttp-2.1.0-cp34-cp34m-manylinux1_i686.whl (433.8 kB view details)

Uploaded CPython 3.4m

File details

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

File metadata

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

File hashes

Hashes for aiohttp-2.1.0.tar.gz
Algorithm Hash digest
SHA256 3e80d944e9295b1360e422d89746b99e23a99118420f826f990a632d284e21df
MD5 52c94bf1735485d9e02fd097ff7d7db9
BLAKE2b-256 cf4abec3705f07294d9a4057fe5abd93ca89f52149931301674e1e6a9dd66366

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for aiohttp-2.1.0-cp36-cp36m-win_amd64.whl
Algorithm Hash digest
SHA256 88efaf9c9a3c5510e285bca0245156187f1e1d7fcd7b53682d09a592c5981375
MD5 c710d6d4cd15db2684dd057e743efe57
BLAKE2b-256 0f088c19f170797769627a73bcba9914004e84a0ee0bb344029cb34bc003390b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for aiohttp-2.1.0-cp36-cp36m-win32.whl
Algorithm Hash digest
SHA256 d9fe9c417ce9ff3ca56fcba9af62a23f31364f935a1e3dba9e6695118dc4072c
MD5 3ae942d030d8a7ef73c78e3fb9d571a3
BLAKE2b-256 1a62b44f988ed2dd993b803ae9c08a07053333c592eb3fdcafb96c63c6accd47

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for aiohttp-2.1.0-cp36-cp36m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 f26aab1480ada5b80e963f4f9bce870d3d5bd0c1e883b7e0d9c622ea190418d5
MD5 04f08e76125840d31fbf16d49cb7d122
BLAKE2b-256 fed7df8b17f2f2ed460ad67a35656c5e62d28ab983bb0f55c136ff58f5ace395

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for aiohttp-2.1.0-cp36-cp36m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 dbc24980b4d6c7f669909e476de135e677bcab3a2ff699fff2ff2d74868aa44e
MD5 839cada4bf41865307f94b504f2893a0
BLAKE2b-256 6159de41ce812acce08b24183f7076764015b721fbc7025c01ae6708c1ae9084

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for aiohttp-2.1.0-cp35-cp35m-win_amd64.whl
Algorithm Hash digest
SHA256 932d42f114b690e05248170f563fdf3d5e35d581fd7b2f2fecc8eaef3b7f2fbc
MD5 3f4670fb62bd92ab368d068bd8f7e769
BLAKE2b-256 f3c441dc17ee874f0973096c3aa79c6e9b115820bd707cf1018b6b0b75c11fd6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for aiohttp-2.1.0-cp35-cp35m-win32.whl
Algorithm Hash digest
SHA256 c85e0494988067a91b57388cdbbcf5a4c31213b919fd72b73ba941f29e08014c
MD5 c403b020ad9ab439177f48a712bff02f
BLAKE2b-256 031586ed07661a1436077e9da1f5fc48d5abe4350dc082b8384155ccf3b12935

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for aiohttp-2.1.0-cp35-cp35m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 36a12a8373bc50cac00d3577820c6f14e7ac3aea301f72d954a6ae17b73629ef
MD5 f426531661dd88b3acd29cda987fe9ca
BLAKE2b-256 22ab8bb0a7d214a30238303c6959ae3d8a06fbfa1e2f0047be96a595ff376852

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for aiohttp-2.1.0-cp35-cp35m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 d5cf7c61df6544af81eda5f43020a5a02e9f1c7ec52fd0f2b113397bd5034911
MD5 51817e4b7809a8c3b04e55151e1194f6
BLAKE2b-256 bb6e4420a6e254e5957c428d37a27eea3f6510d1a57733362da60041f37d69c5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for aiohttp-2.1.0-cp34-cp34m-win_amd64.whl
Algorithm Hash digest
SHA256 b3fb47c9f8772e5e31a55244f99751650b7efb11f3f381ad596d978fb906856b
MD5 d7ff912b5c712002fefca28c685c664f
BLAKE2b-256 804eb6d4a137d706063ba7e91609787f42ab8d0d67e324de7c49cc2680b7de69

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for aiohttp-2.1.0-cp34-cp34m-win32.whl
Algorithm Hash digest
SHA256 9db769467be396f8569d88964b3b4d1e1a1002d8d420dd057123a2c7b2f95adc
MD5 10e3edf9f4e70f0ea8a06fa58b8db988
BLAKE2b-256 ec01cb15675d9bbca54f67cc176bc098d839357bf5053ff737860c4c02083098

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for aiohttp-2.1.0-cp34-cp34m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 40e8132f064618f08ba8797267c805f57a8b1c4f32f08bf5c6964e9a2dd4f4a8
MD5 d4c166df38dd7c6cfb037b87fea9f07f
BLAKE2b-256 8d4c08e5b8432f87a2e35e4cf1b947859750d95976f129db335e91c4e2f888f2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for aiohttp-2.1.0-cp34-cp34m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 a286c122822410338cae3bb6b87c389d13151c5d3c781e17213f8b89ebef3602
MD5 1bb0dd6da744b431ff53943a88c0d1a3
BLAKE2b-256 cba84d90644b60a166c7389b89e16dcb97b3df88a748c419bba5692417fe090a

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