http client/server for asyncio
Project description
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.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
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
Built Distributions
Hashes for aiohttp-1.1.3-cp35-cp35m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 762c25cc7a003ab0e2b376ff21d57fa3a15d52bcaf8d08ffa61dc7d3f5591446 |
|
MD5 | 4c0e719c10bb1487f4b1837c6e1c32fe |
|
BLAKE2b-256 | 595e0b076a7c2e64d42ec3c1e9a64960778c959c0eee1aa91a1dd5d7c63a887f |
Hashes for aiohttp-1.1.3-cp35-cp35m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2ec7b8fc201a6d0163207a664a9c3c6759e0a6abdb8d4518fccd319fa0389d88 |
|
MD5 | 30214c870201b68710f1a9b86c5d865b |
|
BLAKE2b-256 | 72b807be9e39240cea1240db41349e2334e71df7e248b28502366bbe634bd798 |
Hashes for aiohttp-1.1.3-cp35-cp35m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fb23c78f7226d0af4c21a18001ba3b72be785c36e0d11031bcb6d4914ea948d3 |
|
MD5 | 3c91e0d6e2d64512150ca3adc9c38c96 |
|
BLAKE2b-256 | 5752bf0bb2ee68957954d5e5315e7c5955dd08d5353217aff8f3db5fa88662fc |
Hashes for aiohttp-1.1.3-cp35-cp35m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 41543567870b6e0e041b7adbe3bd8d4de58d01e330067ebbe3d51ae6f6cf29ef |
|
MD5 | 2429a30ebd6de834e14c4e36440e466e |
|
BLAKE2b-256 | 841cd80f469537765f76d7db1dfa5d51ea4ba695a13506a3966a1705bf2b6226 |
Hashes for aiohttp-1.1.3-cp34-cp34m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1c0e1f175021996b7547ddcf7df3f8f3d6ff4b34f7f20b8fc9411b51fd716fe0 |
|
MD5 | 08e3be8371e6d34d26d39e3190ee4c4e |
|
BLAKE2b-256 | 4f894b4c6ce6afaea260de6d146bde9317d50d9f78f68e58301759fc181f7e79 |
Hashes for aiohttp-1.1.3-cp34-cp34m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c3fa6ee54a2061fa65677447d17348ea5125cb466444f4f6a92e4ba4ba44d76b |
|
MD5 | 4945cb1e88e9f88f51eca46ccaf57e5a |
|
BLAKE2b-256 | 86b0a4e00245abb3e01727b6fcb65f1c0f34a3b90bf496140eb7f4816ad426e2 |
Hashes for aiohttp-1.1.3-cp34-cp34m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7ad2f89e1a6847100cef4f15302755c4cbf525a67035d222b90e2efcc2315653 |
|
MD5 | fc33c1fdc1fde42d2ef80e2e858d6d21 |
|
BLAKE2b-256 | 00ccc78311aad9f5ec91b2b826327477371c7b904e735148793e52b943f506b6 |
Hashes for aiohttp-1.1.3-cp34-cp34m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5cf620f0d39437216809e164bae58e3549c4d5ea29a6a3d204b38f3ab84eb91d |
|
MD5 | 312a80ea560eacce607d08dee5c91cc7 |
|
BLAKE2b-256 | 20c8051f7c13f3afe6c488e652e742de7fdf84160886b5c562abf34b05de6d24 |