Skip to main content

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

Metadata

Release files for aiohttp 1.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for aiohttp 1.1.0
File Size Uploaded
aiohttp-1.1.0.tar.gz 508.5 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for aiohttp 1.1.0
File
aiohttp-1.1.0-cp35-cp35m-win_amd64.whl CPython 3.5 CPython 3.5 pymalloc Windows x86-64 Details
aiohttp-1.1.0-cp35-cp35m-win32.whl CPython 3.5 CPython 3.5 pymalloc Windows x86-32 Details
aiohttp-1.1.0-cp35-cp35m-manylinux1_x86_64.whl CPython 3.5 CPython 3.5 pymalloc Linux glibc 2.5+ x86-64 Details
aiohttp-1.1.0-cp35-cp35m-manylinux1_i686.whl CPython 3.5 CPython 3.5 pymalloc Linux glibc 2.5+ x86-32 Details
aiohttp-1.1.0-cp34-cp34m-win_amd64.whl CPython 3.4 CPython 3.4 pymalloc Windows x86-64 Details
aiohttp-1.1.0-cp34-cp34m-win32.whl CPython 3.4 CPython 3.4 pymalloc Windows x86-32 Details
aiohttp-1.1.0-cp34-cp34m-manylinux1_x86_64.whl CPython 3.4 CPython 3.4 pymalloc Linux glibc 2.5+ x86-64 Details
aiohttp-1.1.0-cp34-cp34m-manylinux1_i686.whl CPython 3.4 CPython 3.4 pymalloc Linux glibc 2.5+ x86-32 Details

Total release size: 1.7 MB

Release files / aiohttp-1.1.0.tar.gz

Download URL aiohttp-1.1.0.tar.gz
Size 508.5 kB
Tags Source
SHA-256 checksum
How to use checksums
7a33afa92c5eab20252aba4aa184b6ec206bb718fcb51d1d0566d91f8b0dc9be
BLAKE2b-256 checksum
How to use checksums
3f7e7035a96a1d5a68636d47ea48d0abda0e25994ef092167fd5dd12fe380cde
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / aiohttp-1.1.0-cp35-cp35m-win_amd64.whl

Download URL aiohttp-1.1.0-cp35-cp35m-win_amd64.whl
Size 136.4 kB
Tags CPython 3.5 CPython 3.5 pymalloc Windows x86-64
SHA-256 checksum
How to use checksums
1112a1b7b45f000a08f497fdff0ff97abcb02a2ef28dc19038af98261426a1d0
BLAKE2b-256 checksum
How to use checksums
663e549cdc5bdf6d444001b3f9e450226d3e878eb1b392182342f53718d07d99
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / aiohttp-1.1.0-cp35-cp35m-win32.whl

Download URL aiohttp-1.1.0-cp35-cp35m-win32.whl
Size 135.1 kB
Tags CPython 3.5 CPython 3.5 pymalloc Windows x86-32
SHA-256 checksum
How to use checksums
c5144a89ed314d5435434294ca5bd4cd2a24c791158fb36695cdfb8970489209
BLAKE2b-256 checksum
How to use checksums
a57b73e7b75090fa1aebaf2768330d14775013ddf40fab6b5c244444fae0a4fe
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / aiohttp-1.1.0-cp35-cp35m-manylinux1_x86_64.whl

Download URL aiohttp-1.1.0-cp35-cp35m-manylinux1_x86_64.whl
Size 153.2 kB
Tags CPython 3.5 CPython 3.5 pymalloc Linux glibc 2.5+ x86-64
SHA-256 checksum
How to use checksums
c05b95448deabdba71c1c28f778479f4c63140345f5635946f35b3211087f2b3
BLAKE2b-256 checksum
How to use checksums
9f21f7e5005b10443cbb1de87b30235e19ce5e1c7ef5c90baa9d315e03e458c3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / aiohttp-1.1.0-cp35-cp35m-manylinux1_i686.whl

Download URL aiohttp-1.1.0-cp35-cp35m-manylinux1_i686.whl
Size 150.6 kB
Tags CPython 3.5 CPython 3.5 pymalloc Linux glibc 2.5+ x86-32
SHA-256 checksum
How to use checksums
cf5f6bd15edbfc21daf03e7d8eaa6fb452c05500b534b0999fade28a65b15973
BLAKE2b-256 checksum
How to use checksums
da0dcc605614f69c7369ed27006c3f5944a53c07a57aecae0e7ad826373cb3d2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / aiohttp-1.1.0-cp34-cp34m-win_amd64.whl

Download URL aiohttp-1.1.0-cp34-cp34m-win_amd64.whl
Size 134.3 kB
Tags CPython 3.4 CPython 3.4 pymalloc Windows x86-64
SHA-256 checksum
How to use checksums
20ca5752c5cd141f56ede1bc2817a7c2046af2f3b05d84f5510e4ffcb3e1b66b
BLAKE2b-256 checksum
How to use checksums
33f3828906ed9005dd965a988f6d94ad2dc13c92977f96c681416eba272e501a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / aiohttp-1.1.0-cp34-cp34m-win32.whl

Download URL aiohttp-1.1.0-cp34-cp34m-win32.whl
Size 133.9 kB
Tags CPython 3.4 CPython 3.4 pymalloc Windows x86-32
SHA-256 checksum
How to use checksums
ab005bbbfa8f0a980ecebcd8a632fdc6407298f1562f74cf01e566c0fabaf3aa
BLAKE2b-256 checksum
How to use checksums
802b23d819a0fdcad6cd306af3e2d9eeb091f244d48ed0bef362440c06c85b2a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / aiohttp-1.1.0-cp34-cp34m-manylinux1_x86_64.whl

Download URL aiohttp-1.1.0-cp34-cp34m-manylinux1_x86_64.whl
Size 153.4 kB
Tags CPython 3.4 CPython 3.4 pymalloc Linux glibc 2.5+ x86-64
SHA-256 checksum
How to use checksums
1276040895ab085827daa2c5a667caffce3531299a7679fda92ed50039b7d188
BLAKE2b-256 checksum
How to use checksums
418deefa631c900b5d2b0ace40ec101ee4c8f2079d954a1263984cd33ba14224
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / aiohttp-1.1.0-cp34-cp34m-manylinux1_i686.whl

Download URL aiohttp-1.1.0-cp34-cp34m-manylinux1_i686.whl
Size 150.7 kB
Tags CPython 3.4 CPython 3.4 pymalloc Linux glibc 2.5+ x86-32
SHA-256 checksum
How to use checksums
06d751fbe6e50990df022babc5b289508da02af40045cd2cef0a44666bb7112a
BLAKE2b-256 checksum
How to use checksums
55c964145ad328a11747a59da4b7cc0e48f513921159b0223ad22023c559b99d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

3.9.5

76 release files

3.9.4

76 release files

3.9.3

76 release files

3.9.2

76 release files

3.9.1

76 release files

3.9.0

76 release files

3.8.5

87 release files

3.8.4

87 release files

3.8.3

87 release files

3.8.2

87 release files

3.8.1

72 release files

3.8.0

72 release files

3.7.4

37 release files

3.7.3

37 release files

3.7.2

33 release files

3.7.1

33 release files

3.7.0

33 release files

3.6.3

13 release files

3.6.1

13 release files

3.5.4

22 release files

3.5.3

22 release files

3.5.1

22 release files

3.5.0

22 release files

3.4.1

22 release files

3.4.0

22 release files

3.3.2

15 release files

3.3.0

9 release files

3.2.1

15 release files

3.1.3

15 release files

3.1.1

15 release files

3.1.0

15 release files

3.0.9

15 release files

3.0.8

14 release files

3.0.5

15 release files

3.0.4

15 release files

3.0.3

15 release files

3.0.2

15 release files

3.0.1

15 release files

3.0.0

11 release files

2.3.9

16 release files

2.3.8

16 release files

2.3.7

22 release files

2.3.5

22 release files

2.3.4

22 release files

2.3.3

22 release files

2.3.1

22 release files

2.3.0

13 release files

2.2.0

13 release files

2.1.0

13 release files

2.0.7

8 release files

2.0.5

13 release files

2.0.4

13 release files

2.0.3

13 release files

2.0.2

13 release files

2.0.1

13 release files

2.0.0

13 release files

1.3.5

7 release files

1.3.4

7 release files

1.3.3

13 release files

1.3.2

13 release files

1.3.0

9 release files

1.2.0

9 release files

1.1.6

9 release files

1.1.5

9 release files

1.1.4

9 release files

1.1.3

9 release files

1.1.2

9 release files

1.1.1

9 release files

This release

1.1.0 This release

9 release files

1.0.5

9 release files

1.0.3

9 release files

1.0.2

9 release files

1.0.1

9 release files

1.0.0

9 release files

0.22.4

9 release files

0.22.3

9 release files

0.22.2

9 release files

0.22.1

9 release files

0.22.0

9 release files

0.21.5

5 release files

0.21.4

5 release files

0.21.2

5 release files

0.21.1

5 release files

0.20.2

1 release file

0.20.1

1 release file

0.20.0

1 release file

0.19.0

1 release file

0.18.4

1 release file

0.18.3

1 release file

0.18.2

1 release file

0.18.1

1 release file

0.18.0

1 release file

0.17.4

1 release file

0.17.3

1 release file

0.17.2

1 release file

0.17.1

1 release file

0.17.0

1 release file

0.16.6

1 release file

0.16.5

1 release file

0.16.4

1 release file

0.16.3

1 release file

0.16.2

1 release file

0.16.1

1 release file

0.16.0

1 release file

0.15.3

1 release file

0.15.2

1 release file

0.15.1

1 release file

0.15.0

1 release file

0.14.4

1 release file

0.14.3

1 release file

0.14.2

1 release file

0.14.1

1 release file

0.14.0

1 release file

0.13.1

2 release files

0.13.0

2 release files

0.12.0

1 release file

0.11.0

2 release files

0.10.2

2 release files

0.10.1

2 release files

0.10.0

2 release files

0.9.3

2 release files

0.9.2

2 release files

0.9.1

1 release file

0.9.0

1 release file

0.8.4

1 release file

0.8.3

1 release file

0.8.2

1 release file

0.8.1

1 release file

0.8.0

1 release file

0.7.3

1 release file

0.7.2

1 release file

0.7.1

1 release file

0.7.0

1 release file

0.6.5

1 release file

0.6.4

1 release file

0.6.3

1 release file

0.6.2

1 release file

0.6.1

1 release file

0.6.0

1 release file

0.5.0

1 release file

0.4.4

1 release file

0.4.3

1 release file

0.4.2

1 release file

0.4.1

1 release file

0.4

1 release file

0.3

1 release file

0.2

1 release file

0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page