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.2.0 (2016-12-17)

  • Extract BaseRequest from web.Request, introduce web.Server (former RequestHandlerFactory), introduce new low-level web server which is not coupled with web.Application and routing #1362

  • Make TestServer.make_url compatible with yarl.URL #1389

  • Implement range requests for static files #1382

  • Support task attribute for StreamResponse #1410

  • Drop TestClient.app property, use TestClient.server.app instead (BACKWARD INCOMPATIBLE)

  • Drop TestClient.handler property, use TestClient.server.handler instead (BACKWARD INCOMPATIBLE)

  • TestClient.server property returns a test server instance, was asyncio.AbstractServer (BACKWARD INCOMPATIBLE)

  • Follow gunicorn’s signal semantics in Gunicorn[UVLoop]WebWorker #1201

  • Call worker_int and worker_abort callbacks in Gunicorn[UVLoop]WebWorker #1202

  • Has functional tests for client proxy #1218

  • Fix bugs with client proxy target path and proxy host with port #1413

  • Fix bugs related to the use of unicode hostnames #1444

  • Preserve cookie quoting/escaping #1453

  • FileSender will send gzipped response if gzip version available #1426

  • Don’t override Content-Length header in web.Response if no body was set #1400

  • Introduce router.post_init() for solving #1373

  • Fix raise error in case of multiple calls of TimeServive.stop()

  • Allow to raise web exceptions on router resolving stage #1460

  • Add a warning for session creation outside of coroutine #1468

  • Avoid a race when application might start accepting incoming requests but startup signals are not processed yet e98e8c6

  • Raise a RuntimeError when trying to change the status of the HTTP response after the headers have been sent #1480

  • Fix bug with https proxy acquired cleanup #1340

  • Use UTF-8 as the default encoding for multipart text parts #1484

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

Uploaded Source

Built Distributions

aiohttp-1.2.0-cp35-cp35m-win_amd64.whl (138.3 kB view details)

Uploaded CPython 3.5m Windows x86-64

aiohttp-1.2.0-cp35-cp35m-win32.whl (137.1 kB view details)

Uploaded CPython 3.5m Windows x86

aiohttp-1.2.0-cp35-cp35m-manylinux1_x86_64.whl (155.2 kB view details)

Uploaded CPython 3.5m

aiohttp-1.2.0-cp35-cp35m-manylinux1_i686.whl (152.5 kB view details)

Uploaded CPython 3.5m

aiohttp-1.2.0-cp34-cp34m-win_amd64.whl (136.2 kB view details)

Uploaded CPython 3.4m Windows x86-64

aiohttp-1.2.0-cp34-cp34m-win32.whl (135.9 kB view details)

Uploaded CPython 3.4m Windows x86

aiohttp-1.2.0-cp34-cp34m-manylinux1_x86_64.whl (155.3 kB view details)

Uploaded CPython 3.4m

aiohttp-1.2.0-cp34-cp34m-manylinux1_i686.whl (152.6 kB view details)

Uploaded CPython 3.4m

File details

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

File metadata

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

File hashes

Hashes for aiohttp-1.2.0.tar.gz
Algorithm Hash digest
SHA256 8ce0ab4301bf38db2ae13e4921bdb3841507919c9121373405a43c7bd0a07f78
MD5 a7b25d51bf887ec4f9b87463a6bff6d4
BLAKE2b-256 e5a5ade96691f6423c56f7911a42a51ecd5454619efd6d5026df7e08a556a36a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for aiohttp-1.2.0-cp35-cp35m-win_amd64.whl
Algorithm Hash digest
SHA256 f28cebb9439ad17ea09d3c7078559b60cc581e46b2e1894ac300fb896d4ff6d1
MD5 26c314f2b30e105e7d64cccee7673a13
BLAKE2b-256 3a50fed51844e340cb3e5ca52dc07651bad9c8496ef17eca075016fd9f352313

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for aiohttp-1.2.0-cp35-cp35m-win32.whl
Algorithm Hash digest
SHA256 6f7552d70c271e3d881fd70d176d2c5606572797a0f6789e8ebb8af5deb83e89
MD5 ccbb815dddaf2837e932bf500bcb7c34
BLAKE2b-256 4482a63cba1261abe636d93d62be8c37bc83a4ca033e2b67b5949e17d61121cd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for aiohttp-1.2.0-cp35-cp35m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 f7d68b17e0267ddd90406cdc8ca68a501e000b614702e916625df448b94c490f
MD5 a97f7471ae6c72c1b774b8bf98b562a5
BLAKE2b-256 bb4d8e57306aa8b7386054056957b6c73ffb51ef6aeb3b5d2f83acb1e4f8577a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for aiohttp-1.2.0-cp35-cp35m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 4ebd81d92d4f3fda89572864466536e8b085acdf4a1edf4567110538e3def2eb
MD5 3f28ee44ff624f305f379b1b751a16a2
BLAKE2b-256 d5fe6e9025c03acab96181e0b88ce1fac782de451cc694baab39a1943eb56f47

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for aiohttp-1.2.0-cp34-cp34m-win_amd64.whl
Algorithm Hash digest
SHA256 daec40ea3652e85ee6bbf5fe0151fee342c4c5fb9fcd19f007397be06d53e82d
MD5 de305f40b4090e64d7d532e398dc51bd
BLAKE2b-256 4e4242e3fa31990007ee8b1ed4a0952eb509c50822ec8a51a6b5d32de1c6a00d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for aiohttp-1.2.0-cp34-cp34m-win32.whl
Algorithm Hash digest
SHA256 3bfc8c4a91d8c52543253e9374d2185174b09b6e0b4c2a660ec451d6893e34cf
MD5 0d85729f22b2148a9412c522686f98ed
BLAKE2b-256 b701abe338b164c8e82c663aa7121cc645813ed7f896b8ed9491207d88b0c933

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for aiohttp-1.2.0-cp34-cp34m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 a679b1396a311f623f3918a0fb2015c287dcc1c2214be62f17d824e8a95b55ef
MD5 eb9b3b065ce78e5ffd1985067027edc9
BLAKE2b-256 0ea2a4d6bc2227206a68f45053f83d4ef69312886ee3feb9e919006675c13db5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for aiohttp-1.2.0-cp34-cp34m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 688711d00877ef000fcc16b19fd2c7635ce60a24fd4f6624e7ca8324bb91a648
MD5 95736bf6d2ff6a1517421eff6ebe6b7b
BLAKE2b-256 f4ca27d486a5a7a1af62ba7ce731b8e5888ad53373ed7a6fdcf41faae1870a8a

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