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 hashes)

Uploaded Source

Built Distributions

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

Uploaded CPython 3.5m Windows x86-64

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

Uploaded CPython 3.5m Windows x86

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

Uploaded CPython 3.5m

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

Uploaded CPython 3.5m

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

Uploaded CPython 3.4m Windows x86-64

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

Uploaded CPython 3.4m Windows x86

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

Uploaded CPython 3.4m

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

Uploaded CPython 3.4m

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