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.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
Metadata
Release files for aiohttp 1.1.4
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.4.tar.gz | 510.1 kB | Details |
Built distributions (wheels)
| File | Reset | |||
|---|---|---|---|---|
| aiohttp-1.1.4-cp35-cp35m-win_amd64.whl | CPython 3.5 | CPython 3.5 pymalloc | Windows x86-64 | Details |
| aiohttp-1.1.4-cp35-cp35m-win32.whl | CPython 3.5 | CPython 3.5 pymalloc | Windows x86-32 | Details |
| aiohttp-1.1.4-cp35-cp35m-manylinux1_x86_64.whl | CPython 3.5 | CPython 3.5 pymalloc | Linux glibc 2.5+ x86-64 | Details |
| aiohttp-1.1.4-cp35-cp35m-manylinux1_i686.whl | CPython 3.5 | CPython 3.5 pymalloc | Linux glibc 2.5+ x86-32 | Details |
| aiohttp-1.1.4-cp34-cp34m-win_amd64.whl | CPython 3.4 | CPython 3.4 pymalloc | Windows x86-64 | Details |
| aiohttp-1.1.4-cp34-cp34m-win32.whl | CPython 3.4 | CPython 3.4 pymalloc | Windows x86-32 | Details |
| aiohttp-1.1.4-cp34-cp34m-manylinux1_x86_64.whl | CPython 3.4 | CPython 3.4 pymalloc | Linux glibc 2.5+ x86-64 | Details |
| aiohttp-1.1.4-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.4.tar.gz
| Download URL | aiohttp-1.1.4.tar.gz |
|---|---|
| Size | 510.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
11397e2dfdda688d98d9a22f822234c1f2b2071f661f4a8b261ca346e2103eaf
|
|
BLAKE2b-256 checksum How to use checksums |
cfbceaf04ca05c42fa2716a46bcac5e4b0dcadf2032f6c6bd3a36418e14aede7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Release files / aiohttp-1.1.4-cp35-cp35m-win_amd64.whl
| Download URL | aiohttp-1.1.4-cp35-cp35m-win_amd64.whl |
|---|---|
| Size | 137.0 kB |
| Tags | CPython 3.5 CPython 3.5 pymalloc Windows x86-64 |
|
SHA-256 checksum How to use checksums |
fc9391a7540493f5dd9b16c3e623134dd7c3bf75afdb3af4f3cfc0af6bb644c1
|
|
BLAKE2b-256 checksum How to use checksums |
1b5d9a72b64d36cf9a364cf3303d97e64fd9f2313e07f5da82ca4a280bff86aa
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Release files / aiohttp-1.1.4-cp35-cp35m-win32.whl
| Download URL | aiohttp-1.1.4-cp35-cp35m-win32.whl |
|---|---|
| Size | 135.8 kB |
| Tags | CPython 3.5 CPython 3.5 pymalloc Windows x86-32 |
|
SHA-256 checksum How to use checksums |
c7264182af0475e91226a7de566b52f494a62dbd3727e8388579449ab64e2cf4
|
|
BLAKE2b-256 checksum How to use checksums |
5ea9b95453ce072f19e1893e4b604989deafe904350c117a69ba1f7d2cccaa9a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Release files / aiohttp-1.1.4-cp35-cp35m-manylinux1_x86_64.whl
| Download URL | aiohttp-1.1.4-cp35-cp35m-manylinux1_x86_64.whl |
|---|---|
| Size | 153.9 kB |
| Tags | CPython 3.5 CPython 3.5 pymalloc Linux glibc 2.5+ x86-64 |
|
SHA-256 checksum How to use checksums |
d173e4c88f291e1d57b3bbca760270e9635a1267db7e795483050b0af40484fe
|
|
BLAKE2b-256 checksum How to use checksums |
976fdf160dd0b217dbede247acf0850cd08d921f5f41199ad5e96876e00aa598
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Release files / aiohttp-1.1.4-cp35-cp35m-manylinux1_i686.whl
| Download URL | aiohttp-1.1.4-cp35-cp35m-manylinux1_i686.whl |
|---|---|
| Size | 151.2 kB |
| Tags | CPython 3.5 CPython 3.5 pymalloc Linux glibc 2.5+ x86-32 |
|
SHA-256 checksum How to use checksums |
00f4f3c55eabb42db5fb5b4e34faa4a38e042281ebb40d35a70bbefd95db4aed
|
|
BLAKE2b-256 checksum How to use checksums |
f999e61a501867f47033dd1a27c3f507ec087f68d0f669f70b29cff42ea29ee1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Release files / aiohttp-1.1.4-cp34-cp34m-win_amd64.whl
| Download URL | aiohttp-1.1.4-cp34-cp34m-win_amd64.whl |
|---|---|
| Size | 134.9 kB |
| Tags | CPython 3.4 CPython 3.4 pymalloc Windows x86-64 |
|
SHA-256 checksum How to use checksums |
fca5b62281dd3a310ed485abaef1672943eaa59cbb113d79c61b04df4289beae
|
|
BLAKE2b-256 checksum How to use checksums |
78754d3262e11e9f346d4d0d41004dc66e384b0610fe7f765bdb44234c123b44
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Release files / aiohttp-1.1.4-cp34-cp34m-win32.whl
| Download URL | aiohttp-1.1.4-cp34-cp34m-win32.whl |
|---|---|
| Size | 134.6 kB |
| Tags | CPython 3.4 CPython 3.4 pymalloc Windows x86-32 |
|
SHA-256 checksum How to use checksums |
d48adcf2e2a2f5c830823f4df96c982bc60eed0140e3770223cf843c25d8221e
|
|
BLAKE2b-256 checksum How to use checksums |
d7e17cbf59d88d710d2323239d891a4cc3c92b3b9b691dcf957079d2ba632b1c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Release files / aiohttp-1.1.4-cp34-cp34m-manylinux1_x86_64.whl
| Download URL | aiohttp-1.1.4-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 |
17c7d2dc125c1e5a38b48a77633daf1f7482bd1f2f5cf89b88208d0ba031ee91
|
|
BLAKE2b-256 checksum How to use checksums |
51ac2c8368207b53157de413bd0a0ad3a77d03a67f40d4d4cd9a0e600d482688
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Release files / aiohttp-1.1.4-cp34-cp34m-manylinux1_i686.whl
| Download URL | aiohttp-1.1.4-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 |
49210d0a2480b62c3aaba12132898acd35a29e94f55d3a40b20345716b6c7623
|
|
BLAKE2b-256 checksum How to use checksums |
82589cb238d4720c6d013418b5f70d58c110f057a58ae774aec913eca73eebc7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |