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.1.2 (2016-11-08)

  • Allow starting variables with an underscore #1379

  • Properly process UNIX sockets by gunicorn worker #1375

  • Fix ordering for FrozenList

  • Don’t propagate pre and post signals to sub-application #1377

1.1.1 (2016-11-04)

  • Fix documentation generation #1120

1.1.0 (2016-11-03)

  • Drop deprecated WSClientDisconnectedError (BACKWARD INCOMPATIBLE)

  • Use yarl.URL in client API. The change is 99% backward compatible but ClientResponse.url is an yarl.URL instance now. #1217

  • Close idle keep-alive connections on shutdown #1222

  • Modify regex in AccessLogger to accept underscore and numbers #1225

  • Use yarl.URL in web server API. web.Request.rel_url and web.Request.url are added. URLs and templates are percent-encoded now. #1224

  • Accept yarl.URL by server redirections #1278

  • Return yarl.URL by .make_url() testing utility #1279

  • Properly format IPv6 addresses by aiohttp.web.run_app #1139

  • Use yarl.URL by server API #1288

    • Introduce resource.url_for(), deprecate resource.url().

    • Implement StaticResource.

    • Inherit SystemRoute from AbstractRoute

    • Drop old-style routes: Route, PlainRoute, DynamicRoute, StaticRoute, ResourceAdapter.

  • Revert resp.url back to str, introduce resp.url_obj #1292

  • Raise ValueError if BasicAuth login has a “:” character #1307

  • Fix bug when ClientRequest send payload file with opened as open(‘filename’, ‘r+b’) #1306

  • Enhancement to AccessLogger (pass extra dict) #1303

  • Show more verbose message on import errors #1319

  • Added save and load functionality for CookieJar #1219

  • Added option on StaticRoute to follow symlinks #1299

  • Force encoding of application/json content type to utf-8 #1339

  • Fix invalid invocations of errors.LineTooLong #1335

  • Websockets: Stop async for iteration when connection is closed #1144

  • Ensure TestClient HTTP methods return a context manager #1318

  • Raise ClientDisconnectedError to FlowControlStreamReader read function if ClientSession object is closed by client when reading data. #1323

  • Document deployment without Gunicorn #1120

  • Add deprecation warning for MD5 and SHA1 digests when used for fingerprint of site certs in TCPConnector. #1186

  • Implement sub-applications #1301

  • Don’t inherit web.Request from dict but implement MutableMapping protocol.

  • Implement frozen signals

  • Don’t inherit web.Application from dict but implement MutableMapping protocol.

  • Support freezing for web applications

  • Accept access_log parameter in web.run_app, use None to disable logging

  • Don’t flap tcp_cork and tcp_nodelay in regular request handling. tcp_nodelay is still enabled by default.

  • Improve performance of web server by removing premature computing of Content-Type if the value was set by web.Response constructor.

    While the patch boosts speed of trivial web.Response(text=’OK’, content_type=’text/plain) very well please don’t expect significant boost of your application – a couple DB requests and business logic is still the main bottleneck.

  • Boost performance by adding a custom time service #1350

  • Extend ClientResponse with content_type and charset properties like in web.Request. #1349

  • Disable aiodns by default #559

  • Don’t flap tcp_cork in client code, use TCP_NODELAY mode by default.

  • Implement web.Request.clone() #1361

Project details


Release history Release notifications | RSS feed

This version

1.1.2

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

Uploaded Source

Built Distributions

aiohttp-1.1.2-cp35-cp35m-win_amd64.whl (136.7 kB view details)

Uploaded CPython 3.5m Windows x86-64

aiohttp-1.1.2-cp35-cp35m-win32.whl (135.5 kB view details)

Uploaded CPython 3.5m Windows x86

aiohttp-1.1.2-cp35-cp35m-manylinux1_x86_64.whl (153.6 kB view details)

Uploaded CPython 3.5m

aiohttp-1.1.2-cp35-cp35m-manylinux1_i686.whl (150.9 kB view details)

Uploaded CPython 3.5m

aiohttp-1.1.2-cp34-cp34m-win_amd64.whl (134.6 kB view details)

Uploaded CPython 3.4m Windows x86-64

aiohttp-1.1.2-cp34-cp34m-win32.whl (134.3 kB view details)

Uploaded CPython 3.4m Windows x86

aiohttp-1.1.2-cp34-cp34m-manylinux1_x86_64.whl (153.7 kB view details)

Uploaded CPython 3.4m

aiohttp-1.1.2-cp34-cp34m-manylinux1_i686.whl (151.0 kB view details)

Uploaded CPython 3.4m

File details

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

File metadata

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

File hashes

Hashes for aiohttp-1.1.2.tar.gz
Algorithm Hash digest
SHA256 16f16dc5ddb1d5676452f35abb58190ff034198d4e97770e0f59b99ca6d76c2d
MD5 32d575c36d1d7e29ff7d1aa225ad1a69
BLAKE2b-256 cc5e1eb03bc43c482f0987b2c533488e98e007284d15098f97b326dcb739bcff

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for aiohttp-1.1.2-cp35-cp35m-win_amd64.whl
Algorithm Hash digest
SHA256 002b3fb2a4302fecbf67e99841c199c0aa2ffc6cbe658134a486a3fb985ac395
MD5 76ae30e70838eeaaf37b0a3a30093004
BLAKE2b-256 616dc6aa5cc502e47906e2af3cda75cde17b2309830f0d93a3f02d4fca9202d3

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for aiohttp-1.1.2-cp35-cp35m-win32.whl
Algorithm Hash digest
SHA256 7914926079ffd955d091687c6cbe51fff34cbd2e712d1d323f987888f0b54e43
MD5 25a9464cfc5755b9a099b39c325a386f
BLAKE2b-256 16f1a3c4830f2d5e1fae2a9e762ed9266703c69c610f5016730e0cb6d8fcc7b3

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for aiohttp-1.1.2-cp35-cp35m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 c68fd96a7083cfea78f2ecd8e14c1800808e8ec96332de583c9c8bb561ac5851
MD5 8dd0a5be9187482a22ad530b6c592e16
BLAKE2b-256 be05311570db3f739d16e6029d2a3e386fc5aa68a71b8a2fef35a72cb2ae064a

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for aiohttp-1.1.2-cp35-cp35m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 c5dc16a9419fadc3fdc27f27dd82fe9b96360871cad7fd80d944f6cb93c73602
MD5 52b84f2e47e19bb00df4aeeddacc9f24
BLAKE2b-256 261f683c3ad92c0b81034f198874232cfde292aa4d9393aea03910ac2e07da21

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for aiohttp-1.1.2-cp34-cp34m-win_amd64.whl
Algorithm Hash digest
SHA256 833de9a7f0d67c60f7705c7a66856208f97fc679e686043e2e50b7a6bc4735fb
MD5 d24d3e5c62c3b74b46078adf8c7ff777
BLAKE2b-256 a10d279975d898d22c5c91d435661417f6d3aaa4af9d7a34a54a5930d3e73e76

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for aiohttp-1.1.2-cp34-cp34m-win32.whl
Algorithm Hash digest
SHA256 5c1ca4ed13ed3fde44755ec42cff0faea70c88220f68b94cc5f44ef43e1e94f7
MD5 ae56101254667c02cc81dc88ca6fb281
BLAKE2b-256 c921cd5de104e19d025ddfe31c90fbcc7a8610333b69c3c45d8261fc8bab407d

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for aiohttp-1.1.2-cp34-cp34m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 63a014ab43e3df8438b9b8dd37723fb7f4afda6fe791c482651c65c79cb04eb3
MD5 9be8c40be198354254d53a5cb9cdd169
BLAKE2b-256 cb4f9cb0f69c9125f34b2707709f59f2329089ebe6dc4e9ef4f3c2a5002a8799

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for aiohttp-1.1.2-cp34-cp34m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 cd7a553e09fe937ac913c1a193403be7b36c801a0764338c08a32bf219d64038
MD5 478f9b9c0595de7970ffffe78d2a5287
BLAKE2b-256 aaadfc29d9782085e85d94742fd1809e9cac3338269841c583ec0a0ae3a08d2b

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