Skip to main content

http client/server for asyncio

Project description

http client/server for asyncio

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

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.

Source code

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

1.3.3 (2017-02-19)

  • Fixed memory leak in time service #1656

1.3.2 (2017-02-16)

  • Awaiting on WebSocketResponse.send_* does not work #1645

  • Fix multiple calls to client ws_connect when using a shared header dict #1643

  • Make CookieJar.filter_cookies() accept plain string parameter. #1636

1.3.1 (2017-02-09)

  • Handle CLOSING in WebSocketResponse.__anext__

  • Fixed AttributeError ‘drain’ for server websocket handler #1613

1.3.0 (2017-02-08)

  • Multipart writer validates the data on append instead of on a request send #920

  • Multipart reader accepts multipart messages with or without their epilogue to consistently handle valid and legacy behaviors #1526 #1581

  • Separate read + connect + request timeouts # 1523

  • Do not swallow Upgrade header #1587

  • Fix polls demo run application #1487

  • Ignore unknown 1XX status codes in client #1353

  • Fix sub-Multipart messages missing their headers on serialization #1525

  • Do not use readline when reading the content of a part in the multipart reader #1535

  • Add optional flag for quoting FormData fields #916

  • 416 Range Not Satisfiable if requested range end > file size #1588

  • Having a : or @ in a route does not work #1552

  • Added receive_timeout timeout for websocket to receive complete message. #1325

  • Added heartbeat parameter for websocket to automatically send ping message. #1024 #777

  • Remove web.Application dependency from web.UrlDispatcher #1510

  • Accepting back-pressure from slow websocket clients #1367

  • Do not pause transport during set_parser stage #1211

  • Lingering close doesn’t terminate before timeout #1559

  • setsockopt may raise OSError exception if socket is closed already #1595

  • Lots of CancelledError when requests are interrupted #1565

  • Allow users to specify what should happen to decoding errors when calling a responses text() method #1542

  • Back port std module http.cookies for python3.4.2 #1566

  • Maintain url’s fragment in client response #1314

  • Allow concurrently close WebSocket connection #754

  • Gzipped responses with empty body raises ContentEncodingError #609

  • Return 504 if request handle raises TimeoutError.

  • Refactor how we use keep-alive and close lingering timeouts.

  • Close response connection if we can not consume whole http message during client response release

  • Abort closed ssl client transports, broken servers can keep socket open un-limit time #1568

  • Log warning instead of RuntimeError is websocket connection is closed.

  • Deprecated: aiohttp.protocol.HttpPrefixParser will be removed in 1.4 #1590

  • Deprecated: Servers response’s .started, .start() and .can_start() method will be removed in 1.4 #1591

  • Deprecated: Adding sub app via app.router.add_subapp() is deprecated use app.add_subapp() instead, will be removed in 1.4 #1592

  • Deprecated: aiohttp.get(), aiohttp.options(), aiohttp.head(), aiohttp.post(), aiohttp.put(), aiohttp.patch(), aiohttp.delete(), and aiohttp.ws_connect() will be removed in 1.4 #1593

  • Deprecated: Application.finish() and Application.register_on_finish() will be removed in 1.4 #1602

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

Uploaded Source

Built Distributions

aiohttp-1.3.3-cp36-cp36m-win_amd64.whl (150.3 kB view details)

Uploaded CPython 3.6m Windows x86-64

aiohttp-1.3.3-cp36-cp36m-win32.whl (149.1 kB view details)

Uploaded CPython 3.6m Windows x86

aiohttp-1.3.3-cp36-cp36m-manylinux1_x86_64.whl (167.4 kB view details)

Uploaded CPython 3.6m

aiohttp-1.3.3-cp36-cp36m-manylinux1_i686.whl (164.7 kB view details)

Uploaded CPython 3.6m

aiohttp-1.3.3-cp35-cp35m-win_amd64.whl (150.3 kB view details)

Uploaded CPython 3.5m Windows x86-64

aiohttp-1.3.3-cp35-cp35m-win32.whl (149.1 kB view details)

Uploaded CPython 3.5m Windows x86

aiohttp-1.3.3-cp35-cp35m-manylinux1_x86_64.whl (167.2 kB view details)

Uploaded CPython 3.5m

aiohttp-1.3.3-cp35-cp35m-manylinux1_i686.whl (164.5 kB view details)

Uploaded CPython 3.5m

aiohttp-1.3.3-cp34-cp34m-win_amd64.whl (148.2 kB view details)

Uploaded CPython 3.4m Windows x86-64

aiohttp-1.3.3-cp34-cp34m-win32.whl (147.9 kB view details)

Uploaded CPython 3.4m Windows x86

aiohttp-1.3.3-cp34-cp34m-manylinux1_x86_64.whl (167.4 kB view details)

Uploaded CPython 3.4m

aiohttp-1.3.3-cp34-cp34m-manylinux1_i686.whl (164.7 kB view details)

Uploaded CPython 3.4m

File details

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

File metadata

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

File hashes

Hashes for aiohttp-1.3.3.tar.gz
Algorithm Hash digest
SHA256 103433f594442b98ed4af9db02331e548a0ef2d4cfd02207ea24ed9eb85862e4
MD5 880e6744e7d125331467b8d3e2637392
BLAKE2b-256 dac1b95412e44f81622a8aa743bddcd28f8192ccf5db81d228ab7d713d6ec9e8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for aiohttp-1.3.3-cp36-cp36m-win_amd64.whl
Algorithm Hash digest
SHA256 ad088e3248f0d24e5fe32cb4b4fba26a9eee7ed279486167f199f3e6801c327d
MD5 191519b7ad94d80be07385a45cfb9144
BLAKE2b-256 873a59b1cf8c92506cdad64ff9123e75305848aaba2c74e8af2fcf0d12fc8e56

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for aiohttp-1.3.3-cp36-cp36m-win32.whl
Algorithm Hash digest
SHA256 8eba325bf3da75569d2aeaf2350b0395e52f565a15b290a2449660a2d15dd33f
MD5 901c8fae3c5921e79160b9cdddc57cb8
BLAKE2b-256 2584bece0fa84522c3058254a0384ba48f6cd35df23ff29385d10a8b8ec54d6f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for aiohttp-1.3.3-cp36-cp36m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 93fb44f50b1385b2c143b25f0c695a7cf82ecd5bcac6a326a3ddc7a7c4e138e9
MD5 7d6dcd2e6ce8b9dffd111d4380800a27
BLAKE2b-256 aa1608172f0a8be97e6b4d81e89afa70ff5f52d98d357c9ea3a1b221e48b3f75

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for aiohttp-1.3.3-cp36-cp36m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 3c58339b8817f13397fd64e958aec10b4aebaf5514fc747f63d6ed4714b30d46
MD5 6ffe5e6da7953d6121c8168a6cbf0093
BLAKE2b-256 57d16cd29c05edfdd19bf36dfba1d5a4e69e4e5147ebda5091b0a656515dddb4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for aiohttp-1.3.3-cp35-cp35m-win_amd64.whl
Algorithm Hash digest
SHA256 ea3cf8a14ea2e219ccd38f0166fd1afb568d689ed35d250f34477e6799801236
MD5 8e60617711b00942b3f620a5590ebc3d
BLAKE2b-256 3ca04cb100b2d642fa1b653a8a39b729ac124a68b7fb6d46a4341155ae27479f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for aiohttp-1.3.3-cp35-cp35m-win32.whl
Algorithm Hash digest
SHA256 bd46cb66a7e01179af2ab6885566e8d12d938ee5d4bcc0bbc4dbc13e95a7c46c
MD5 179d44010fab49c4e680c9d2025f4d1f
BLAKE2b-256 644131fafd9acd94810eb417112c40789ed04c3594211dd549c218ea01299df6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for aiohttp-1.3.3-cp35-cp35m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 331bca78048022e52ab99a3049bd72ae18953c5a3e9549265489afcfce065a9c
MD5 92e38b84964380ef3c8957fccb7fa464
BLAKE2b-256 646f817da9651f73536ad6138427617742052e1f8614c3f968467c6ab629cc3f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for aiohttp-1.3.3-cp35-cp35m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 0bbb12e9fac2c8365f4831710bca35a00f4ce3ecd52c22b36620fcc72b32f059
MD5 b4bbdd6b3739f4eee8ca905eb624dfc2
BLAKE2b-256 89181c4b3eab61f6aaf140aa30817f46fa63c1fad703a9b095e9827442b4e9a7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for aiohttp-1.3.3-cp34-cp34m-win_amd64.whl
Algorithm Hash digest
SHA256 0a480c3dc8e0f55decf72497dbd5a53601a4ee1526b6dfe085ea9c3cf5c00ebe
MD5 e84047eb84f5e52d94e73b64dd58b70e
BLAKE2b-256 9d675de6676176944babd7ec952569c307959928caebfd0799bcbd05e84f3692

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for aiohttp-1.3.3-cp34-cp34m-win32.whl
Algorithm Hash digest
SHA256 bb8af23f2b886a067bb7ffc38e77aad6e5580986a2620a4739f22804a8510e3e
MD5 750bd9578660d5e7c96ab2e1c0339663
BLAKE2b-256 98c5b3ea1ac3b9f3399daa45be1b9e31ac9a165bd59913e6ab6d02da98792ce4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for aiohttp-1.3.3-cp34-cp34m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 4cccbac9a7ec7c26fe8c223febb697850b7ca191e1092fe5286c6de32f2ab86b
MD5 8b5ea26c08ef4d8b910d86bf6d96eeef
BLAKE2b-256 ae3bc5a5602f98d8f945bd995039e0c15624cfc961a28824c42bdb56cdfa3fff

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for aiohttp-1.3.3-cp34-cp34m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 8f71fbd7d2dbf21d686117400224296fc12f5e0b14a0697797b39258b6f39510
MD5 bcaf18332e2e6e87a146539705e1b0e1
BLAKE2b-256 b14a5c73248b6431448141913315237bbb34263fddac925224c05b380ff49939

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