http client/server for asyncio
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
Discussion list
aio-libs google group: https://groups.google.com/forum/#!forum/aio-libs
Requirements
Python >= 3.4.2
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.5 (2016-11-16)
Fix static file serving in fallback mode #1401
1.1.4 (2016-11-14)
Make TestServer.make_url compatible with yarl.URL #1389
Generate informative exception on redirects from server which doesn’t provide redirection headers #1396
1.1.3 (2016-11-10)
Support root resources for sub-applications #1379
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
Release files for aiohttp 1.1.5
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| aiohttp-1.1.5.tar.gz | 510.2 kB | Details |
Built distributions (wheels)
| File | Reset | |||
|---|---|---|---|---|
| aiohttp-1.1.5-cp35-cp35m-win_amd64.whl | CPython 3.5 | CPython 3.5 pymalloc | Windows x86-64 | Details |
| aiohttp-1.1.5-cp35-cp35m-win32.whl | CPython 3.5 | CPython 3.5 pymalloc | Windows x86-32 | Details |
| aiohttp-1.1.5-cp35-cp35m-manylinux1_x86_64.whl | CPython 3.5 | CPython 3.5 pymalloc | Linux glibc 2.5+ x86-64 | Details |
| aiohttp-1.1.5-cp35-cp35m-manylinux1_i686.whl | CPython 3.5 | CPython 3.5 pymalloc | Linux glibc 2.5+ x86-32 | Details |
| aiohttp-1.1.5-cp34-cp34m-win_amd64.whl | CPython 3.4 | CPython 3.4 pymalloc | Windows x86-64 | Details |
| aiohttp-1.1.5-cp34-cp34m-win32.whl | CPython 3.4 | CPython 3.4 pymalloc | Windows x86-32 | Details |
| aiohttp-1.1.5-cp34-cp34m-manylinux1_x86_64.whl | CPython 3.4 | CPython 3.4 pymalloc | Linux glibc 2.5+ x86-64 | Details |
| aiohttp-1.1.5-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.5.tar.gz
| Download URL | aiohttp-1.1.5.tar.gz |
|---|---|
| Size | 510.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e6c5a8008ab8bbdb706034bedc91835ed820cdc2367ddea9142697612908df84
|
|
BLAKE2b-256 checksum How to use checksums |
5f60afb29b5712ade524efdce339e2a6a0cb69c44115804ab5d4e976bf3f1983
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Release files / aiohttp-1.1.5-cp35-cp35m-win_amd64.whl
| Download URL | aiohttp-1.1.5-cp35-cp35m-win_amd64.whl |
|---|---|
| Size | 137.1 kB |
| Tags | CPython 3.5 CPython 3.5 pymalloc Windows x86-64 |
|
SHA-256 checksum How to use checksums |
91a5d6f5f8083e0339e02f21a59d873dadd2a003c5c5bb360cce2d23155980ca
|
|
BLAKE2b-256 checksum How to use checksums |
5db6deaa802e95f6433a4b61ae2d3aef12f1eb00e2f3200076f491f5d0461279
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Release files / aiohttp-1.1.5-cp35-cp35m-win32.whl
| Download URL | aiohttp-1.1.5-cp35-cp35m-win32.whl |
|---|---|
| Size | 135.9 kB |
| Tags | CPython 3.5 CPython 3.5 pymalloc Windows x86-32 |
|
SHA-256 checksum How to use checksums |
4a2ac388457d55861e42a123d5307cd8ea6d02a8665065daee19ab7239a9e61d
|
|
BLAKE2b-256 checksum How to use checksums |
f6dc34db27e6ff34fede41c08cc6e563dc3b48818c0e2c53eb36b1cfbf8b5e83
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Release files / aiohttp-1.1.5-cp35-cp35m-manylinux1_x86_64.whl
| Download URL | aiohttp-1.1.5-cp35-cp35m-manylinux1_x86_64.whl |
|---|---|
| Size | 154.0 kB |
| Tags | CPython 3.5 CPython 3.5 pymalloc Linux glibc 2.5+ x86-64 |
|
SHA-256 checksum How to use checksums |
9bdc4fe5b4907f4a80caf46b173fe6fd3ca138477f9220ab22d009c328c6edf2
|
|
BLAKE2b-256 checksum How to use checksums |
de8dacbdd6862d7a94e08855ad1c18964e6370a4fb8c58fd92e0fbeb152882d1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Release files / aiohttp-1.1.5-cp35-cp35m-manylinux1_i686.whl
| Download URL | aiohttp-1.1.5-cp35-cp35m-manylinux1_i686.whl |
|---|---|
| Size | 151.3 kB |
| Tags | CPython 3.5 CPython 3.5 pymalloc Linux glibc 2.5+ x86-32 |
|
SHA-256 checksum How to use checksums |
af3d548eea3dc366c0559a7f43820d7f4d8cba0be824b2bbdaef95be9e1361e4
|
|
BLAKE2b-256 checksum How to use checksums |
08a5cb7e38f43fa974efcbc6c72291df154b6623d16c27f2037e04cae75dde73
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Release files / aiohttp-1.1.5-cp34-cp34m-win_amd64.whl
| Download URL | aiohttp-1.1.5-cp34-cp34m-win_amd64.whl |
|---|---|
| Size | 135.0 kB |
| Tags | CPython 3.4 CPython 3.4 pymalloc Windows x86-64 |
|
SHA-256 checksum How to use checksums |
d034760f4fc3ff8fa61fe41ca7d36e8782e49d7f90e7567484a11afd7e0fb4ee
|
|
BLAKE2b-256 checksum How to use checksums |
b6e43dd60792bf4978f1c2af335cd964661f5f19f780a4fd7b640a66b9273bfc
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Release files / aiohttp-1.1.5-cp34-cp34m-win32.whl
| Download URL | aiohttp-1.1.5-cp34-cp34m-win32.whl |
|---|---|
| Size | 134.7 kB |
| Tags | CPython 3.4 CPython 3.4 pymalloc Windows x86-32 |
|
SHA-256 checksum How to use checksums |
97f6f974029432a42e65c7084b8c4221baaf8a1be0eb5c4817be5538017a76ab
|
|
BLAKE2b-256 checksum How to use checksums |
7e5f2a2ec3d0d2dd4f801091993040421d897afb0004828d67345dcdf7423cee
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Release files / aiohttp-1.1.5-cp34-cp34m-manylinux1_x86_64.whl
| Download URL | aiohttp-1.1.5-cp34-cp34m-manylinux1_x86_64.whl |
|---|---|
| Size | 154.1 kB |
| Tags | CPython 3.4 CPython 3.4 pymalloc Linux glibc 2.5+ x86-64 |
|
SHA-256 checksum How to use checksums |
374832ea71c292a221e0bf7e0dde193db19807a1286dca0183eb7bfa794479f9
|
|
BLAKE2b-256 checksum How to use checksums |
35ca9a7279d1cd5b442496d078d10db3a9db929d9281e29e374b4bd9801c74de
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Release files / aiohttp-1.1.5-cp34-cp34m-manylinux1_i686.whl
| Download URL | aiohttp-1.1.5-cp34-cp34m-manylinux1_i686.whl |
|---|---|
| Size | 151.4 kB |
| Tags | CPython 3.4 CPython 3.4 pymalloc Linux glibc 2.5+ x86-32 |
|
SHA-256 checksum How to use checksums |
f9bf35edb7e1483e06f6c887ed2a7a823acdb8c8f0daf64d08d7cafe628a0aec
|
|
BLAKE2b-256 checksum How to use checksums |
4dedcfd300ec84a0164d30fac7ed9bf409fc247113f6650d4d3fddcec40cb9d2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |