Async http client/server framework (asyncio)
Project description
Async http client/server framework
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
import async_timeout
async def fetch(session, url):
with async_timeout.timeout(10):
async with session.get(url) as response:
return await response.text()
async def main():
async with aiohttp.ClientSession() as session:
html = await fetch(session, 'http://python.org')
print(html)
if __name__ == '__main__':
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
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
External links
Feel free to make a Pull Request for adding your link to these pages!
Communication channels
aio-libs google group: https://groups.google.com/forum/#!forum/aio-libs
Feel free to post your questions and ideas here.
gitter chat https://gitter.im/aio-libs/Lobby
We support Stack Overflow. Please add aiohttp tag to your question there.
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.5 (2017-08-03)
Don’t raise deprecation warning on loop.run_until_complete(client.close()) (#2065)
2.2.4 (2017-08-02)
Fix issue with synchronous session closing when using ClientSession as an asynchronous context manager. (#2063)
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. (#1953)Added 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 Distributions
Built Distributions
Hashes for aiohttp-2.3.0a2-cp36-cp36m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f0d3946f991540b8dafd54fa59c69a07cd1d78b0ccae9e0957a4e117a26a0741 |
|
MD5 | febb3e2a2a79763a6d9e753969a29b72 |
|
BLAKE2b-256 | 8feb0d63268466b030717aa0f1884c3644e893be97ad5d6adbc8aa710edb1bf7 |
Hashes for aiohttp-2.3.0a2-cp36-cp36m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 010704667f1605b1b6503a1cd1dca59a9a72e59f23d74794dd358c9169708a09 |
|
MD5 | 55d1308d3857886e86d606b665efd97d |
|
BLAKE2b-256 | afc3d3c910d6ee2711e27b00a3e308a339c46b5bf0a6b157e39b85ac1883274a |
Hashes for aiohttp-2.3.0a2-cp36-cp36m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fd904856172c815a193d6034d46a41aae7a5f64241e63101baefc8096669b8e7 |
|
MD5 | 453d983edd64e2d458e140446fcd14e6 |
|
BLAKE2b-256 | 4859da0dda1989844b3b545fb1d8f0129be036ef490d9ba100f14d60c63b96de |
Hashes for aiohttp-2.3.0a2-cp36-cp36m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 023d18b81b5a057b6a8d2116a80a7018ac1b62bc1453eb7bad15de2a688f848a |
|
MD5 | 0a8b132124961c2a0dba083b2b449b0f |
|
BLAKE2b-256 | 376545623da61d18c16f4b62fe6e5e686a8b2966c67955d3e99aa25b31381009 |
Hashes for aiohttp-2.3.0a2-cp35-cp35m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8eea1039295f524cca8362a5cafae1eb1cda9a6f66c786e8e0c8cf8b69613b17 |
|
MD5 | 1b4127b27ce041652970d6cc0acd1cbf |
|
BLAKE2b-256 | b92da9c63185c5f055546ef16b919fb11f33b51e8aaecd964d65c5ddb6a926b2 |
Hashes for aiohttp-2.3.0a2-cp35-cp35m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c67a1d404ab903062a3a9cab689ab7d032f85bdc12e916c099fe2dc2014d3897 |
|
MD5 | 6a044c6a585e1096c8d326356357b5df |
|
BLAKE2b-256 | 763106c2676c88bf239e20754e7acbe5c77c884a48b4ff0be712e15e38259cdb |
Hashes for aiohttp-2.3.0a2-cp35-cp35m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 80a214f2006cea004d9ba1ce65397de360bd5f6fc643753f6e514e4333881e82 |
|
MD5 | e951b6dd9c9d5a22fdb0b2fa28184b63 |
|
BLAKE2b-256 | 7aa8813aa677323098af15ffc03edf5ea542b2ff9406443a4f9797c5492e0153 |
Hashes for aiohttp-2.3.0a2-cp35-cp35m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0ac3d41f92c255c17fc8ed82b6754238f55880e80172f81cc7cbdde2312b04de |
|
MD5 | 1d198eb8545686bb63cf1a6601ce9f52 |
|
BLAKE2b-256 | 8191fc8d8c5536dce0c89d485deef699b5b2db7aad065f0dd9cbd9e37abc12fb |
Hashes for aiohttp-2.3.0a2-cp34-cp34m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | db4b8e4f75b5cfa7bc0897387624c2c03f57ae82f9a191c0c1bf1f85c43b4c6e |
|
MD5 | 334a94de8e91da18a5a96693df345ee5 |
|
BLAKE2b-256 | 8b93577274a15d9e836f103e84053a64793e599c2a6f63f1c57daff3d3ef8149 |
Hashes for aiohttp-2.3.0a2-cp34-cp34m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d3df35c33ee1b5c6295986047f81bb39051d68a873a336f40988073614148876 |
|
MD5 | 539c5765bbbd5d0e43f88e1f9b4a0d6c |
|
BLAKE2b-256 | 1076c086eb7bc55710a3304df02961c23c9a9f5064fd0c1a0e4878da73cce055 |
Hashes for aiohttp-2.3.0a2-cp34-cp34m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4133f1d0176ce7fb9318f3ce634da1d7c0b2084dba1db00024985f4cab60d572 |
|
MD5 | b881b298f706c8193e0a3faf66d75d96 |
|
BLAKE2b-256 | 88bbba27ab2c7a546317245671180a153b0f7169b20f7b67196a0a71ef452e6c |
Hashes for aiohttp-2.3.0a2-cp34-cp34m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | baf087afeda7fd28aebf180c92b5e71637fde2419a0c0284a6a8bc27ad715735 |
|
MD5 | 14cc0b2d1a3851223b945a89d30c0f23 |
|
BLAKE2b-256 | 94443676da1fc6b436363210c4fe69f588d3209e1c8f750d8278e176ff426741 |