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.0.3 (2016-09-22)

  • Fix FlowControlStreamReader.read_nowait so that it checks whether the transport is paused #1206

1.0.2 (2016-09-22)

  • Make CookieJar compatible with 32-bit systems #1188

  • Add missing WSMsgType to web_ws.__all__, see #1200

  • Fix CookieJar ctor when called with loop=None #1203

  • Fix broken upper-casing in wsgi support #1197

1.0.1 (2016-09-16)

  • Restore aiohttp.web.MsgType alias for aiohttp.WSMsgType for sake of backward compatibility #1178

  • Tune alabaster schema.

  • Use text/html content type for displaying index pages by static file handler.

  • Fix AssertionError in static file handling #1177

  • Fix access log formats %O and %b for static file handling

  • Remove debug setting of GunicornWorker, use app.debug to control its debug-mode instead

1.0.0 (2016-09-16)

  • Change default size for client session’s connection pool from unlimited to 20 #977

  • Add IE support for cookie deletion. #994

  • Remove deprecated WebSocketResponse.wait_closed method (BACKWARD INCOMPATIBLE)

  • Remove deprecated force parameter for ClientResponse.close method (BACKWARD INCOMPATIBLE)

  • Avoid using of mutable CIMultiDict kw param in make_mocked_request #997

  • Make WebSocketResponse.close a little bit faster by avoiding new task creating just for timeout measurement

  • Add proxy and proxy_auth params to client.get() and family, deprecate ProxyConnector #998

  • Add support for websocket send_json and receive_json, synchronize server and client API for websockets #984

  • Implement router shourtcuts for most useful HTTP methods, use app.router.add_get(), app.router.add_post() etc. instead of app.router.add_route() #986

  • Support SSL connections for gunicorn worker #1003

  • Move obsolete examples to legacy folder

  • Switch to multidict 2.0 and title-cased strings #1015

  • {FOO}e logger format is case-sensitive now

  • Fix logger report for unix socket 8e8469b

  • Rename aiohttp.websocket to aiohttp._ws_impl

  • Rename aiohttp.MsgType tp aiohttp.WSMsgType

  • Introduce aiohttp.WSMessage officially

  • Rename Message -> WSMessage

  • Remove deprecated decode param from resp.read(decode=True)

  • Use 5min default client timeout #1028

  • Relax HTTP method validation in UrlDispatcher #1037

  • Pin minimal supported asyncio version to 3.4.2+ (loop.is_close() should be present)

  • Remove aiohttp.websocket module (BACKWARD INCOMPATIBLE) Please use high-level client and server approaches

  • Link header for 451 status code is mandatory

  • Fix test_client fixture to allow multiple clients per test #1072

  • make_mocked_request now accepts dict as headers #1073

  • Add Python 3.5.2/3.6+ compatibility patch for async generator protocol change #1082

  • Improvement test_client can accept instance object #1083

  • Simplify ServerHttpProtocol implementation #1060

  • Add a flag for optional showing directory index for static file handling #921

  • Define web.Application.on_startup() signal handler #1103

  • Drop ChunkedParser and LinesParser #1111

  • Call Application.startup in GunicornWebWorker #1105

  • Fix client handling hostnames with 63 bytes when a port is given in the url #1044

  • Implement proxy support for ClientSession.ws_connect #1025

  • Return named tuple from WebSocketResponse.can_prepare #1016

  • Fix access_log_format in GunicornWebWorker #1117

  • Setup Content-Type to application/octet-stream by default #1124

  • Deprecate debug parameter from app.make_handler(), use Application(debug=True) instead #1121

  • Remove fragment string in request path #846

  • Use aiodns.DNSResolver.gethostbyname() if available #1136

  • Fix static file sending on uvloop when sendfile is available #1093

  • Make prettier urls if query is empty dict #1143

  • Fix redirects for HEAD requests #1147

  • Default value for StreamReader.read_nowait is -1 from now #1150

  • aiohttp.StreamReader is not inherited from asyncio.StreamReader from now (BACKWARD INCOMPATIBLE) #1150

  • Streams documentation added #1150

  • Add multipart coroutine method for web Request object #1067

  • Publish ClientSession.loop property #1149

  • Fix static file with spaces #1140

  • Fix piling up asyncio loop by cookie expiration callbacks #1061

  • Drop Timeout class for sake of async_timeout external library. aiohttp.Timeout is an alias for async_timeout.timeout

  • use_dns_cache parameter of aiohttp.TCPConnector is True by default (BACKWARD INCOMPATIBLE) #1152

  • aiohttp.TCPConnector uses asynchronous DNS resolver if available by default (BACKWARD INCOMPATIBLE) #1152

  • Conform to RFC3986 - do not include url fragments in client requests #1174

  • Drop ClientSession.cookies (BACKWARD INCOMPATIBLE) #1173

  • Refactor AbstractCookieJar public API (BACKWARD INCOMPATIBLE) #1173

  • Fix clashing cookies with have the same name but belong to different domains (BACKWARD INCOMPATIBLE) #1125

  • Support binary Content-Transfer-Encoding #1169

Metadata

Release files for aiohttp 1.0.3

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.0.3
File Size Uploaded
aiohttp-1.0.3.tar.gz 499.7 kB Details

Built distributions (wheels)

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

Total release size: 1.6 MB

Release files / aiohttp-1.0.3.tar.gz

Download URL aiohttp-1.0.3.tar.gz
Size 499.7 kB
Tags Source
SHA-256 checksum
How to use checksums
5ec0fdf17868bcabfef4effa3ce53c4cfa6108b481585bda88498f46435d5eaa
BLAKE2b-256 checksum
How to use checksums
9e98982208caf31a284f020e9bb3b5f7b13dbd1d122d903b39e91620481de70b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

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

Download URL aiohttp-1.0.3-cp35-cp35m-win_amd64.whl
Size 133.8 kB
Tags CPython 3.5 CPython 3.5 pymalloc Windows x86-64
SHA-256 checksum
How to use checksums
4e019bec120e3c20f33e78a4660405591c904548a06ef783acc7ab8c57199a7b
BLAKE2b-256 checksum
How to use checksums
5a01bd8f765adccd6c092d3294741c52f05925f0e24dff29e56dd965398219d7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

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

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

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

Download URL aiohttp-1.0.3-cp35-cp35m-manylinux1_x86_64.whl
Size 150.6 kB
Tags CPython 3.5 CPython 3.5 pymalloc Linux glibc 2.5+ x86-64
SHA-256 checksum
How to use checksums
04fc641f2bf39aec6fa3dd0179613f593de47e44d45da5475c34b5395cda1922
BLAKE2b-256 checksum
How to use checksums
39e5d00560c04e0c4e10ad2f0ba0b76f97a6a55acc9661194a06921fdfcefd7c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

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

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

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

Download URL aiohttp-1.0.3-cp34-cp34m-win_amd64.whl
Size 131.7 kB
Tags CPython 3.4 CPython 3.4 pymalloc Windows x86-64
SHA-256 checksum
How to use checksums
ebf7a26b83beffbfc94d84eb44e3ef5149e9df190faa9e958d1ef2fc5337174d
BLAKE2b-256 checksum
How to use checksums
55768dfb6b689670d8f2386e8d71380272c9a4b77984009fa34bb3831013c93e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

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

Download URL aiohttp-1.0.3-cp34-cp34m-win32.whl
Size 131.3 kB
Tags CPython 3.4 CPython 3.4 pymalloc Windows x86-32
SHA-256 checksum
How to use checksums
683bb6f580c3dfb54ba9d0ccd6b8ad8b8f54bf474ee2e206bf36fb27eee13a0e
BLAKE2b-256 checksum
How to use checksums
e185e2194f49a878bfed6abdaf6a2b797aba1e27ea779b3900212884dff8b83c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

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

Download URL aiohttp-1.0.3-cp34-cp34m-manylinux1_x86_64.whl
Size 150.8 kB
Tags CPython 3.4 CPython 3.4 pymalloc Linux glibc 2.5+ x86-64
SHA-256 checksum
How to use checksums
269de438fc5a543830818bdc1fe7e304ae6d8f9bf55ee872104d1b22f682f713
BLAKE2b-256 checksum
How to use checksums
4f3d48586709e8b28add9bf4a1e2f61157e13ce4c591f813231c593af8d58d6c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

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

Download URL aiohttp-1.0.3-cp34-cp34m-manylinux1_i686.whl
Size 148.1 kB
Tags CPython 3.4 CPython 3.4 pymalloc Linux glibc 2.5+ x86-32
SHA-256 checksum
How to use checksums
cc6266e9d9b303374c05f7652b671b1afbf7fc3a278e20ef16aa6b229d0e1d59
BLAKE2b-256 checksum
How to use checksums
7cf7419af141fe306c492456859690c8b6f9d1039c25a1208eb3951c27ceca6b
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

1.1.0

9 release files

1.0.5

9 release files

This release

1.0.3 This release

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