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.2.3 (2017-07-04)
Fix _CoroGuard for python 3.4
2.2.2 (2017-07-03)
Allow await session.close() along with yield from session.close()
2.2.1 (2017-07-02)
Relax yarl requirement to 0.11+
Backport #2026: session.close is a coroutine (#2029)
2.2.0 (2017-06-20)
Add doc for add_head, update doc for add_get. #1944
Fixed consecutive calls for Response.write_eof.
Retain method attributes (e.g.
__doc__
) when registering synchronous handlers for resources. #1953Added signal TERM handling in run_app to gracefully exit #1932
Fix websocket issues caused by frame fragmentation. #1962
Raise RuntimeError is you try to set the Content Length and enable chunked encoding at the same time #1941
Small update for unittest_run_loop
Use CIMultiDict for ClientRequest.skip_auto_headers #1970
Fix wrong startup sequence: test server and run_app() are not raise DeprecationWarning now #1947
Make sure cleanup signal is sent if startup signal has been sent #1959
Fixed server keep-alive handler, could cause 100% cpu utilization #1955
Connection can be destroyed before response get processed if await aiohttp.request(..) is used #1981
MultipartReader does not work with -OO #1969
Fixed ClientPayloadError with blank Content-Encoding header #1931
Support deflate encoding implemented in httpbin.org/deflate #1918
Fix BadStatusLine caused by extra CRLF after POST data #1792
Keep a reference to ClientSession in response object #1985
Deprecate undocumented app.on_loop_available signal #1978
2.1.0 (2017-05-26)
Added support for experimental async-tokio event loop written in Rust https://github.com/PyO3/tokio
Write to transport \r\n before closing after keepalive timeout, otherwise client can not detect socket disconnection. #1883
Only call loop.close in run_app if the user did not supply a loop. Useful for allowing clients to specify their own cleanup before closing the asyncio loop if they wish to tightly control loop behavior
Content disposition with semicolon in filename #917
Added request_info to response object and ClientResponseError. #1733
Added history to ClientResponseError. #1741
Allow to disable redirect url re-quoting #1474
Handle RuntimeError from transport #1790
Dropped “%O” in access logger #1673
Added args and kwargs to unittest_run_loop. Useful with other decorators, for example @patch. #1803
Added iter_chunks to response.content object. #1805
Avoid creating TimerContext when there is no timeout to allow compatibility with Tornado. #1817 #1180
Add proxy_from_env to ClientRequest to read from environment variables. #1791
Add DummyCookieJar helper. #1830
Fix assertion errors in Python 3.4 from noop helper. #1847
Do not unquote + in match_info values #1816
Use Forwarded, X-Forwarded-Scheme and X-Forwarded-Host for better scheme and host resolution. #1134
Fix sub-application middlewares resolution order #1853
Fix applications comparison #1866
Fix static location in index when prefix is used #1662
Make test server more reliable #1896
Extend list of web exceptions, add HTTPUnprocessableEntity, HTTPFailedDependency, HTTPInsufficientStorage status codes #1920
2.0.7 (2017-04-12)
Fix pypi distribution
Fix exception description #1807
Handle socket error in FileResponse #1773
Cancel websocket heartbeat on close #1793
2.0.6 (2017-04-04)
Keeping blank values for request.post() and multipart.form() #1765
TypeError in data_received of ResponseHandler #1770
Fix web.run_app not to bind to default host-port pair if only socket is passed #1786
2.0.5 (2017-03-29)
Memory leak with aiohttp.request #1756
Disable cleanup closed ssl transports by default.
Exception in request handling if the server responds before the body is sent #1761
2.0.4 (2017-03-27)
Memory leak with aiohttp.request #1756
Encoding is always UTF-8 in POST data #1750
Do not add “Content-Disposition” header by default #1755
2.0.3 (2017-03-24)
Call https website through proxy will cause error #1745
Fix exception on multipart/form-data post if content-type is not set #1743
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.2.3-cp36-cp36m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 27304c87942dd4d6d412affa3870fbe4e04456bff0b53883f35de15ddf447b85 |
|
MD5 | e0b18f765dfdc59e05846c411c9691a2 |
|
BLAKE2b-256 | b80bfbecba1346d6279aba71418a0dd5dfe08e5bcb9f3926f8f1b7a6c81ac6d1 |
Hashes for aiohttp-2.2.3-cp36-cp36m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c4477474c7ddd931a698c6bd7e3de0b519383d2f2165c7ff4c4e1940fca6703b |
|
MD5 | 104626a6f10539c847520f79e50d70d0 |
|
BLAKE2b-256 | d856859ddc5fcf74e315b14587bf6779be367449fc57095e5734792509bbc3d0 |
Hashes for aiohttp-2.2.3-cp36-cp36m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0e921684726fa79c8d328885cf6131e37983303cf17b5695c98c5e3659f8f1b0 |
|
MD5 | e9e6db70cc07df017984388adafb6bde |
|
BLAKE2b-256 | 1eaf6b0b1e6da49e570f433b7f9fa9b2e9e7b2c2b39a3b1f48f0f7febd2117e2 |
Hashes for aiohttp-2.2.3-cp36-cp36m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 893bafb2562ef0b8860e5cacaed89a84c1f1e01081d99cdce456111b5356b273 |
|
MD5 | 82f9a5953360b22fd4a8badd41bbfb91 |
|
BLAKE2b-256 | 10178fe3e63dfef29783588adb2f21e5b2d563b7259b143b80ccda61d6f8ed87 |
Hashes for aiohttp-2.2.3-cp35-cp35m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9f1aee9002e726c47e9997c9b68df7663964fe486c893397645c38b9b82b60eb |
|
MD5 | 8c8d6cdca7f7ce3ffd23b4c28800b9a7 |
|
BLAKE2b-256 | 678747442bf8be3e7d493b3020f56c9fc26df2e2e43e540db531b657aa1df19d |
Hashes for aiohttp-2.2.3-cp35-cp35m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fc3e4b5ec85e6fbbc89132d1c04a88492dbeba915c1c710879573c7246245250 |
|
MD5 | 48386c6d48d3dd597ae81a45291fca7c |
|
BLAKE2b-256 | 64af9c6afd96bcfac0d9983fa741f3c75dfc451f7c142a1bde7c7d9305b902bf |
Hashes for aiohttp-2.2.3-cp35-cp35m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a08d8857f5ea7e1010eab8f8137538ddca0700070897065e89ec470d7e66d029 |
|
MD5 | bc7f682c3ea75a01f31d34f35daabfa7 |
|
BLAKE2b-256 | 8bf993f8393ff83575d1cbfef65ddde523d77c0e54ca7debd0c52bedf30e5a37 |
Hashes for aiohttp-2.2.3-cp35-cp35m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4156e9257dbeef314e105509eef38dd1f1e831a83eb6101ed5e124d02f4ddf89 |
|
MD5 | cfb3093c0ea44773b1d80e44ec0a4152 |
|
BLAKE2b-256 | fef286913ee1b0ad346f2076785f01cb577674ea2e9b1453f61ae7a593c8ff80 |
Hashes for aiohttp-2.2.3-cp34-cp34m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ddf46cc74d13bd79ce9ae54a5092c04b272a974c8cb7d6859582aa28ba699424 |
|
MD5 | 2871570adbf3d41dc632433a578981c8 |
|
BLAKE2b-256 | 7462e48b2004443b26f5cf9f1399ff096a92ae52ffa0ba63f6c93cb4d9abdaf4 |
Hashes for aiohttp-2.2.3-cp34-cp34m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9e0f22161a1fa079ef4fcd97a19b6e55654375b13473204afb69b81d0e12b58b |
|
MD5 | 0596ec94eefa594fe908df9b0c687d4a |
|
BLAKE2b-256 | e2f408e4ad924bb891ae322f0113d28f6ba7a291bbb3a55217e2449cc4c44a3e |
Hashes for aiohttp-2.2.3-cp34-cp34m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4a13e7588cae72f54df0697422990084e024a5ab2136ffcd70fa190d29220d42 |
|
MD5 | 1ef375b3a941e8359425d7c60c017c4f |
|
BLAKE2b-256 | 7c6ac59b08e32a7dae0f1f68c9297eaa9b27e1f37f797fb00c81f50441c7730f |
Hashes for aiohttp-2.2.3-cp34-cp34m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b941ce5089b4b5b7e4df45229438869080322e733a43725d6b105c8f413ba7ff |
|
MD5 | a18a16b87cad30668c8a3249c749b1dd |
|
BLAKE2b-256 | c9a3a746df9ad93b522e6abffa4af66e9e253e71456ba87533ceaacb5e08ed54 |