Async http client/server framework (asyncio)
Project description
Async http client/server framework
Key Features
Supports both client and server side of HTTP protocol.
Supports both client and server Web-Sockets out-of-the-box without the Callback Hell.
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):
async 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 wshandle(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.add_routes([web.get('/', handle),
web.get('/echo', wshandle),
web.get('/{name}', handle)])
web.run_app(app)
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.5.3
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
Changelog
3.1.1 (2018-03-27)
Support asynchronous iterators (and asynchronous generators as well) in both client and server API as request / response BODY payloads. (#2802)
3.1.0 (2018-03-21)
Welcome to aiohttp 3.1 release.
This is an incremental release, fully backward compatible with aiohttp 3.0.
But we have added several new features.
The most visible one is app.add_routes() (an alias for existing app.router.add_routes(). The addition is very important because all aiohttp docs now uses app.add_routes() call in code snippets. All your existing code still do register routes / resource without any warning but you’ve got the idea for a favorite way: noisy app.router.add_get() is replaced by app.add_routes().
The library does not make a preference between decorators:
routes = web.RouteTableDef() @routes.get('/') async def hello(request): return web.Response(text="Hello, world") app.add_routes(routes)
and route tables as a list:
async def hello(request): return web.Response(text="Hello, world") app.add_routes([web.get('/', hello)])
Both ways are equal, user may decide basing on own code taste.
Also we have a lot of minor features, bug fixes and documentation updates, see below.
Features
Relax JSON content-type checking in the ClientResponse.json() to allow “application/xxx+json” instead of strict “application/json”. (#2206)
Bump C HTTP parser to version 2.8 (#2730)
Accept a coroutine as an application factory in web.run_app and gunicorn worker. (#2739)
Implement application cleanup context (app.cleanup_ctx property). (#2747)
Make writer.write_headers a coroutine. (#2762)
Add tracking signals for getting request/response bodies. (#2767)
Deprecate ClientResponseError.code in favor of .status to keep similarity with response classes. (#2781)
Implement app.add_routes() method. (#2787)
Implement web.static() and RouteTableDef.static() API. (#2795)
Install a test event loop as default by asyncio.set_event_loop(). The change affects aiohttp test utils but backward compatibility is not broken for 99.99% of use cases. (#2804)
Refactor ClientResponse constructor: make logically required constructor arguments mandatory, drop _post_init() method. (#2820)
Use app.add_routes() in server docs everywhere (#2830)
Websockets refactoring, all websocket writer methods are converted into coroutines. (#2836)
Provide Content-Range header for Range requests (#2844)
Bugfixes
Fix websocket client return EofStream. (#2784)
Fix websocket demo. (#2789)
Property BaseRequest.http_range now returns a python-like slice when requesting the tail of the range. It’s now indicated by a negative value in range.start rather then in range.stop (#2805)
Close a connection if an unexpected exception occurs while sending a request (#2827)
Fix firing DNS tracing events. (#2841)
Improved Documentation
Change ClientResponse.json() documentation to reflect that it now allows “application/xxx+json” content-types (#2206)
Document behavior when cchardet detects encodings that are unknown to Python. (#2732)
Add diagrams for tracing request life style. (#2748)
Drop removed functionality for passing StreamReader as data at client side. (#2793)
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-3.1.1-cp36-cp36m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 228ff9359f9dab15f93d9df6623fab495222d96724dca7c5e4852d42b1bac439 |
|
MD5 | 5f9327db49db7867175f9947ad3238d3 |
|
BLAKE2b-256 | 623919c93c453494d9f28e5a5540068106d4e009d0e0b2ca3d411dedefe99581 |
Hashes for aiohttp-3.1.1-cp36-cp36m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a6943101e13106f91d6175dd96bd9097ebbc076799f553e2f8ffc1b811dd190f |
|
MD5 | 96923a06892ba58bca00617bfed6ad83 |
|
BLAKE2b-256 | e9e6336dda9264fe5fed2c1dfefced4c83b4efd241e5cf146da87488685b2669 |
Hashes for aiohttp-3.1.1-cp36-cp36m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a95add5d4305bf30ed624b171cc91bba61b57b22976b8b30930d511a34d05557 |
|
MD5 | 4ef7f2b6c0718a0fb59f0484476e08dd |
|
BLAKE2b-256 | 1787b8ac32d5bdda906236bbf7a543d3910a240cbc1cb9dd7ce75c979ada5735 |
Hashes for aiohttp-3.1.1-cp36-cp36m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9559f4a69132b7aeb116c3ac8029f8bbae48e27292c4ef9c50e94cb872e6d0ce |
|
MD5 | ffaa4ea9c7470bfffd9751af050374f1 |
|
BLAKE2b-256 | a7d22dc567aa193ad2f5beb3caa60538ec4912efa828062a4eaedd684c20c7cd |
Hashes for aiohttp-3.1.1-cp36-cp36m-macosx_10_12_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 389a05544589c0643f0c1ba485c8d5157eb182de4ad35d041f858832a0acb26d |
|
MD5 | 263bb6c86ddbf84b6fc0e5cb417e5035 |
|
BLAKE2b-256 | 4937a11e81314442e1c6045d0851deb2c8684795ac02f03845a993ef624d9a53 |
Hashes for aiohttp-3.1.1-cp36-cp36m-macosx_10_11_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ce6c816ff4ba694f15be0986c12c6200b63f56c3da4c75545cba7c86365e8413 |
|
MD5 | 469c50b2b1448fccb5dd269d7288bf77 |
|
BLAKE2b-256 | 16641e9011e1765b7aa0c86e4335f87f427ee4e66fa2aa5dd59efb62068e17a3 |
Hashes for aiohttp-3.1.1-cp36-cp36m-macosx_10_10_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 56c57f1e6e78b30509b8bc451bd24c4a96014fd874f853f9dbe5896287787bec |
|
MD5 | 18fb97ef6db20901ad94298ea50ecb73 |
|
BLAKE2b-256 | 09194cf220072452cf7d3b3f6c9564c406c5852bb4ca9e79db14f3bda1427d53 |
Hashes for aiohttp-3.1.1-cp35-cp35m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d4e6ec8b45ef7aa0bfe0f7f9703b36e62f72f752c286818cf543d44201df559b |
|
MD5 | 6b9a16f2e9a680e3b8f70dde3effd1c6 |
|
BLAKE2b-256 | e25182ecf0fbff2859478c4ff59b2ecce7ffe531b02b772b9ca1afbd30307ce4 |
Hashes for aiohttp-3.1.1-cp35-cp35m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 67c5bf258adba7f57a30949e0a17c4708f98d91263a0c82da27ad37a8f60e3a9 |
|
MD5 | cb43f0f95ea92d88cdb18e7a65ffd348 |
|
BLAKE2b-256 | 5beccd3cca40c55cf9b50fc3ed146a7eb262fad16a8d55f46917ced87b93e603 |
Hashes for aiohttp-3.1.1-cp35-cp35m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c7ce03728c2fe5a47c72b45164f449b027903143ea720ebf379bc7c76b536d2e |
|
MD5 | 6772ba9666dba0a64fe5c76ad647bf7c |
|
BLAKE2b-256 | a4153d71590909708cb111f910c6329649b1f57f9f7bc3a35d03c499467f385e |
Hashes for aiohttp-3.1.1-cp35-cp35m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1c052bce3eade41f6edf9ff3381051b809a88e65b66dcf2e05d34e9751652ee1 |
|
MD5 | 44b65c9d1cef5155eebdcf54b74dce15 |
|
BLAKE2b-256 | b0b63014ff33e0b8cf8b39cc302341f630eafad40c173ca77c5751320ba7c14f |
Hashes for aiohttp-3.1.1-cp35-cp35m-macosx_10_12_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 42f0237a8314033e7dbb3ac83707bc5e5db28fc300a815d7088f8c52e599f0de |
|
MD5 | 9fbdf9a291ecefa6afc1e61d95391212 |
|
BLAKE2b-256 | 986e8f09fb2c237798e87af1ac777613f7f658f6974effd219694e565df0d878 |
Hashes for aiohttp-3.1.1-cp35-cp35m-macosx_10_11_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8d65a8033cafc0d0268de5a858465c819475f3467d456aab3fbe3912e298e717 |
|
MD5 | d5d1ed424105ed2ca69a6ab75a822db6 |
|
BLAKE2b-256 | c70f7d8aa954e5bce26226231947eb3daf8b37980b674a7c2fe778f7f4ce1a29 |
Hashes for aiohttp-3.1.1-cp35-cp35m-macosx_10_10_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3da537d4cf64c23b69dafdae0fcfca173eedadac19271ea417e189a629c63258 |
|
MD5 | c66b477175bdf9506cbbdc9bfc03dee0 |
|
BLAKE2b-256 | a25117aad7f3ee870c907dd43e515ea5a6e2bdfc384025363b9d5b80e5cd9004 |