Async http client/server framework (asyncio)
Project description
Async http client/server framework
aiohttp 2.0 release!
For this release we completely refactored low-level implementation of http handling. Finally uvloop gives performance improvement. Overall performance improvement should be around 70-90% compared to 1.x version.
We took opportunity to refactor long standing api design problems across whole package. Client exceptions handling has been cleaned up and now much more straight forward. Client payload management simplified and allows to extend with any custom type. Client connection pool implementation has been redesigned as well, now there is no need for actively releasing response objects, aiohttp handles connection release automatically.
Another major change, we moved aiohttp development to public organization https://github.com/aio-libs
With this amount of api changes we had to make backward incompatible changes. Please check this migration document http://aiohttp.readthedocs.io/en/latest/migration.html
Please report problems or annoyance with with api to https://github.com/aio-libs/aiohttp
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:
await ws.send_str("Hello, {}".format(msg.data))
elif msg.type == web.MsgType.binary:
await 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.
Keepsafe
The aiohttp community would like to thank Keepsafe (https://www.getkeepsafe.com) for it’s support in the early days of the project.
Source code
The latest developer version is available in a github repository: https://github.com/aio-libs/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
2.0.2 (2017-03-21)
Fixed Application.on_loop_available signal #1739
Remove debug code
2.0.1 (2017-03-21)
Fix allow-head to include name on route #1737
Fixed AttributeError in WebSocketResponse.can_prepare #1736
2.0.0 (2017-03-20)
Added json to ClientSession.request() method #1726
Added session’s raise_for_status parameter, automatically calls raise_for_status() on any request. #1724
response.json() raises ClientReponseError exception if response’s content type does not match #1723
Cleanup timer and loop handle on any client exception.
Deprecate loop parameter for Application’s constructor
2.0.0rc1 (2017-03-15)
Properly handle payload errors #1710
Added ClientWebSocketResponse.get_extra_info() #1717
It is not possible to combine Transfer-Encoding and chunked parameter, same for compress and Content-Encoding #1655
Connector’s limit parameter indicates total concurrent connections. New limit_per_host added, indicates total connections per endpoint. #1601
Use url’s raw_host for name resolution #1685
Change ClientResponse.url to yarl.URL instance #1654
Add max_size parameter to web.Request reading methods #1133
Web Request.post() stores data in temp files #1469
Add the allow_head=True keyword argument for add_get #1618
run_app and the Command Line Interface now support serving over Unix domain sockets for faster inter-process communication.
run_app now supports passing a preexisting socket object. This can be useful e.g. for socket-based activated applications, when binding of a socket is done by the parent process.
Implementation for Trailer headers parser is broken #1619
Fix FileResponse to not fall on bad request (range out of file size)
Fix FileResponse to correct stream video to Chromes
Deprecate public low-level api #1657
Deprecate encoding parameter for ClientSession.request() method
Dropped aiohttp.wsgi #1108
Dropped version from ClientSession.request() method
Dropped websocket version 76 support #1160
Dropped: aiohttp.protocol.HttpPrefixParser #1590
Dropped: Servers response’s .started, .start() and .can_start() method #1591
Dropped: Adding sub app via app.router.add_subapp() is deprecated use app.add_subapp() instead #1592
Dropped: Application.finish() and Application.register_on_finish() #1602
Dropped: web.Request.GET and web.Request.POST
Dropped: aiohttp.get(), aiohttp.options(), aiohttp.head(), aiohttp.post(), aiohttp.put(), aiohttp.patch(), aiohttp.delete(), and aiohttp.ws_connect() #1593
Dropped: aiohttp.web.WebSocketResponse.receive_msg() #1605
Dropped: ServerHttpProtocol.keep_alive_timeout attribute and keep-alive, keep_alive_on, timeout, log constructor parameters #1606
Dropped: TCPConnector’s` .resolve, .resolved_hosts, .clear_resolved_hosts() attributes and resolve constructor parameter #1607
Dropped ProxyConnector #1609
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-2.0.2-cp36-cp36m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 226b4c0a90590f1a7e8956028dea7a844f33f86fd9f0921820953104d8e76317 |
|
MD5 | d36aa0562ecfdcab5f5e77cd1c3412c5 |
|
BLAKE2b-256 | 38d4c2db825f8a1469adad1fc2f43e88af81c2444c142feb6b32b8ad86ac8f54 |
Hashes for aiohttp-2.0.2-cp36-cp36m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1d5174582945172ef9f6d8e0be723b202700ae7b53b9dc78842d5e0f244398a1 |
|
MD5 | e5d78fba3a9c650d8098bf2be878f1c1 |
|
BLAKE2b-256 | 7eb1f8a58b28b351415f2db7927d288f46e07feea1ea17d130b971e376e01cc8 |
Hashes for aiohttp-2.0.2-cp36-cp36m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ba13417850d3dbd06bceaafa70f240b410539ecc4ba2c60202b7b631f5737800 |
|
MD5 | 8228ebd91048e556a38b2f594ff4d90c |
|
BLAKE2b-256 | f82dde936058e394c7c312ae32a92783977f2177029a9785b7018658dee3437d |
Hashes for aiohttp-2.0.2-cp36-cp36m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 17eaf6fd2d85a7feb14d664c0477eeb90b93b60d4bfa1b1cf8be4b6d14a4aed5 |
|
MD5 | 8fca32ddf1655d87701a347d0bdd0500 |
|
BLAKE2b-256 | 9ffde8eeaef5fefa363bfa678baeecf1257b52d4d0f18846d431c1281dc17e6f |
Hashes for aiohttp-2.0.2-cp35-cp35m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 48d28d5bf6b5393f9ee0ec2b356b4a6fd59308caac847666d29de5fffa608616 |
|
MD5 | fafc8d8ed2bd0dcf220662b6a4980872 |
|
BLAKE2b-256 | a6a57fa0ffca6b60b20fb61ed68a169c86080247bc51536b156650345980b59b |
Hashes for aiohttp-2.0.2-cp35-cp35m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | dc04850e04617a77d07c903431a729275960f648d69ad59b23749a1c73b31ec8 |
|
MD5 | 1578edf7cba66ae3483ec55fbc0857c0 |
|
BLAKE2b-256 | 41b248e21d9fabc9a32c8c25105a7bce3136ecd93fdc328b8532eac31546332d |
Hashes for aiohttp-2.0.2-cp35-cp35m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1cde8cca3d79ce6189e8d19a511afc75f8fcb25d460ae96ecb1a27b629c5154e |
|
MD5 | cda97fb72db84acf6a90767e0e425e70 |
|
BLAKE2b-256 | 9568800da75c498d0cfb9f4fcfb52b5eec2410bba98055b0af7f0554401b1ae0 |
Hashes for aiohttp-2.0.2-cp35-cp35m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | aa817a64bfcb4db772785c4fbeee9fc27db0b1ae1a9f400f75132a674ffd791c |
|
MD5 | 8c052753784864f3c1d7f1f6d42b47b8 |
|
BLAKE2b-256 | 1f3cb7fa5530100ac52921228b1f201c4077966e4f832d3886affe3b61f55f57 |
Hashes for aiohttp-2.0.2-cp34-cp34m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9847c6d312e08f13e728e6edc0a0039c7d7a10b015caf35ac1cf88a3fa612f25 |
|
MD5 | ef3c538eedfefd14a1e027bd6736f080 |
|
BLAKE2b-256 | 879fc5054a3cf1e6a844fb2ed113c2b84acfbe09979fb27ea2661b49dcc4257d |
Hashes for aiohttp-2.0.2-cp34-cp34m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 97e306955cfde2c2dcfadac9a23f5bb652d9d6dc2ecea28b3aabb8461c40131f |
|
MD5 | e8601a0e985c474c10f6d263c75a00bc |
|
BLAKE2b-256 | 5913372137b20e89704d88f84e57646cd3725b4f6fb23cc072c869414fcb2a99 |
Hashes for aiohttp-2.0.2-cp34-cp34m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3819fd6bd1424a346bb06b3377c109cd82390653c4620223571da89dac8949c5 |
|
MD5 | 604a448a56279744e6e446c67612d5f1 |
|
BLAKE2b-256 | 49d686dc72e57b74c21fdefa1c161b5a7866755e137bcb3699923618ae51e9b7 |
Hashes for aiohttp-2.0.2-cp34-cp34m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 37ec6b596c68dd50c51061b987d5278e4dbaa4c6c29f00cfe575363806d95bfb |
|
MD5 | 9d04462afc2752f76ea0fae9fa93323e |
|
BLAKE2b-256 | 86ee52702ddf6af35fea3e5183b8bf8f3b09de41afcd900ab57f5e2af3458706 |