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

This version

1.3.0

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

Uploaded Source

Built Distributions

aiohttp-1.3.0-cp35-cp35m-win_amd64.whl (149.8 kB view details)

Uploaded CPython 3.5m Windows x86-64

aiohttp-1.3.0-cp35-cp35m-win32.whl (148.6 kB view details)

Uploaded CPython 3.5m Windows x86

aiohttp-1.3.0-cp35-cp35m-manylinux1_x86_64.whl (166.7 kB view details)

Uploaded CPython 3.5m

aiohttp-1.3.0-cp35-cp35m-manylinux1_i686.whl (164.0 kB view details)

Uploaded CPython 3.5m

aiohttp-1.3.0-cp34-cp34m-win_amd64.whl (147.7 kB view details)

Uploaded CPython 3.4m Windows x86-64

aiohttp-1.3.0-cp34-cp34m-win32.whl (147.4 kB view details)

Uploaded CPython 3.4m Windows x86

aiohttp-1.3.0-cp34-cp34m-manylinux1_x86_64.whl (166.8 kB view details)

Uploaded CPython 3.4m

aiohttp-1.3.0-cp34-cp34m-manylinux1_i686.whl (164.2 kB view details)

Uploaded CPython 3.4m

File details

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

File metadata

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

File hashes

Hashes for aiohttp-1.3.0.tar.gz
Algorithm Hash digest
SHA256 4bfa7810e35ac9f7bba23b9c2e738d1acdd618471fa492740b4b77f5d317bfb3
MD5 ac5351a77bcfd90d5a87de3d419a8d9a
BLAKE2b-256 23895c703e7a3dfe7796fe24f9c8e82a45f9a544e07b22524c6124e7e67167f7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for aiohttp-1.3.0-cp35-cp35m-win_amd64.whl
Algorithm Hash digest
SHA256 fb6b2a2a4ea1c359e24a76e2997eec0932bb3357c36b0e811a27c5e41429eb25
MD5 ed81b138df8d2df998c544875ca380bf
BLAKE2b-256 2c2d894f7ffb97ca0c827088c8973e775f1671041a1d4ecc3a7b065211013b5b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for aiohttp-1.3.0-cp35-cp35m-win32.whl
Algorithm Hash digest
SHA256 75c74b06e9aa70e9815172d616fa6235c73c8aa0094855058143328fcd1cd369
MD5 9015ef9e1a5e294ed1bb80b55bf28652
BLAKE2b-256 e726e437d502b0dccb22f47a355e6f015eb0f49b009996deec6eaa2691ce3204

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for aiohttp-1.3.0-cp35-cp35m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 5485c44ffae28d4580277d9071eeef0f738d42752e4250837af11e534541f91d
MD5 7712faa70266b69af34496fe7859548c
BLAKE2b-256 e30e13fbba29f19d2541750210541cba4ebb185f9b8084e0315da5826e280414

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for aiohttp-1.3.0-cp35-cp35m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 c415bf633d9017ed957b98754034d145f736e380e307af6d14a684389e8bd69c
MD5 86de744e3b7b264ded264ad0f22b5ded
BLAKE2b-256 dcd0acfe38e781a6ff0fc10c0bdbd8d8b423e7146e35044ee2c40a95bdd2f9f3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for aiohttp-1.3.0-cp34-cp34m-win_amd64.whl
Algorithm Hash digest
SHA256 973ea928154f8f261e0fc2ec4a76ed029bc500cd8b1149436d8648f78f7fa81d
MD5 5d331189cf234990f951dfcbe0fdcc53
BLAKE2b-256 6b7b63a2fb07462980b251960f3a145cdd45d3c18c5098484417450f0e5fea7f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for aiohttp-1.3.0-cp34-cp34m-win32.whl
Algorithm Hash digest
SHA256 938557c0f47a5d344631c6f2e698d93cc105b87ad64f01878e778b0c953e29fb
MD5 fc5f0bf23a1be03ea6fe20628f34b938
BLAKE2b-256 e2c034680840c1c65e6ff83fd6a218a0866b9f4e8049c9e403a77a44e1c29b25

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for aiohttp-1.3.0-cp34-cp34m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 d7b26bd009db4e8dc2fb75073dc86496ba42b4e6cb7fe6d869d255a37624576c
MD5 5ac2b04e28f0f57215297a18790d9c45
BLAKE2b-256 c00b4c20a0ff14c643880dc943c76aba5330b2ca91b37ec266ef4e9ba0de3041

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for aiohttp-1.3.0-cp34-cp34m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 4824249e2e7f1d04046fe9f3a7d019f09029026f21e8f00a19c5df0bc5c1b66a
MD5 db1b2e4eac804260c0b1fd04ccb291f1
BLAKE2b-256 2bfeaf9463cb8405679404c8f090f8da5c0e0a746d72eab42c28b00c564bb64b

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