Skip to main content

http client/server for asyncio

Project description

http client/server for asyncio

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

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

1.3.5 (2017-03-16)

  • Fixed None timeout support #1720

1.3.4 (2017-03-15)

  • Revert timeout handling in client request

  • Fix StreamResponse representation after eof

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

  • Fix file_sender to correct stream video to Chromes

  • Fix NotImplementedError server exception #1703

  • Clearer error message for URL without a host name. #1691

  • Silence deprecation warning in __repr__ #1690

  • IDN + HTTPS = ssl.CertificateError #1685

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

Uploaded Source

Built Distributions

aiohttp-1.3.5-cp36-cp36m-manylinux1_x86_64.whl (168.4 kB view details)

Uploaded CPython 3.6m

aiohttp-1.3.5-cp36-cp36m-manylinux1_i686.whl (165.7 kB view details)

Uploaded CPython 3.6m

aiohttp-1.3.5-cp35-cp35m-manylinux1_x86_64.whl (168.2 kB view details)

Uploaded CPython 3.5m

aiohttp-1.3.5-cp35-cp35m-manylinux1_i686.whl (165.5 kB view details)

Uploaded CPython 3.5m

aiohttp-1.3.5-cp34-cp34m-manylinux1_x86_64.whl (168.4 kB view details)

Uploaded CPython 3.4m

aiohttp-1.3.5-cp34-cp34m-manylinux1_i686.whl (165.7 kB view details)

Uploaded CPython 3.4m

File details

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

File metadata

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

File hashes

Hashes for aiohttp-1.3.5.tar.gz
Algorithm Hash digest
SHA256 cd14a45da385b5e860849ffaff3ecee56f9b37bf9e7f3f7bc5ce3f17556cf842
MD5 fac473aa71b5e49a6978b5bcc393a7ef
BLAKE2b-256 e908721f2e99f4ed694335f533a2910b4c75b1b37a376fe65da07903e0bbe1d2

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for aiohttp-1.3.5-cp36-cp36m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 d1a55e05643e44108fd6cee64ff60500ad952fe0f90e36e16cb68eb2c8cc85b1
MD5 07bf1e50c61811ec8cc2b6a7f67789b8
BLAKE2b-256 b51b284511923d15b2ea9ef930c100a1c57655f459dd2896dc2a2b6e151065b7

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for aiohttp-1.3.5-cp36-cp36m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 d0cfed3fb87935e0703196d9d52b43aa9a92570a2b8cb9d2501cf20b25cf3208
MD5 6c0fb90a854ff42208bf1792963e8da6
BLAKE2b-256 382d0cd562fbb3b6a3c6c1496fbc64fa4c98a57c1071820bea683d2a14fe69f7

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for aiohttp-1.3.5-cp35-cp35m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 fd39fa8bede6ab806b0138f678408e720f6ce1893fd44dfda3dcefd3915f7dca
MD5 48d2b0f984144826d195761134d46e58
BLAKE2b-256 7aa9a4829fb463b6472c470af0c7d375b815352f14b41ec04bf721d195e0d098

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for aiohttp-1.3.5-cp35-cp35m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 2fc496888a49c18903419f28717909def477408d33a49a145573174b8f9a7219
MD5 77d87381bb9e8b61c17764a712713e8a
BLAKE2b-256 69617c564b1410d397160934fd61856efada2cca4689a734018821e023099d76

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for aiohttp-1.3.5-cp34-cp34m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 e922e0be63eb7bf600f2b82f475ab2710415f098218583a1f5e3b7ef573f21d3
MD5 8ff47860d22b1d04001b079553e73032
BLAKE2b-256 c643f14ad6dc9123f4fc9584b6af174e9f11b8ee3f58d5397a1cc25b0027799e

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for aiohttp-1.3.5-cp34-cp34m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 b85f5a411dd994ce815d10461a2d9e490441d1cfde1be621b09146b0357cf4da
MD5 a5cf0991f57013ecf9a7b025031a5577
BLAKE2b-256 8fd8723245a6d04d3d2c592f5aa90342319d8db036712d188b1692c198e28274

See more details on using hashes here.

Provenance

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