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.2 (2018-04-05)
Make LineTooLong exception more detailed about actual data size (#2863)
Call on_chunk_sent when write_eof takes as a param the last chunk (#2909)
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.2-cp36-cp36m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 78b8d80546e0ad09c0b88c38db9c418f63b0912dccc85667e367e853932bad16 |
|
MD5 | 0ef74efd5e70ed5a4e738eb5e6b8f31e |
|
BLAKE2b-256 | d3fd722f191a44de94711dec538f08c063f0e3b7565cbc3235d7c4da19cdcda5 |
Hashes for aiohttp-3.1.2-cp36-cp36m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3208fbf10f7d6b65d80dbd5592f2ec5483651cc61781a20a23c8fdeef8de6d3a |
|
MD5 | 9ac22c22e8fbf1f0e966332369192e02 |
|
BLAKE2b-256 | 3abf2a8947b53c0a90a401a53abd97a8553bc2d72de74bc9b8c9325687869c46 |
Hashes for aiohttp-3.1.2-cp36-cp36m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1d931839a6ae45ea02aa92f05dc68d67fa441a1c53fc1c1b8d01976f1b7276e4 |
|
MD5 | 9f91079570b44af0fe5a1916f3e20743 |
|
BLAKE2b-256 | a1425fb97cb28ed4e1d38a4c4f78a39c3bc8bcdfa6e274949c9cf7bd9a65de32 |
Hashes for aiohttp-3.1.2-cp36-cp36m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | aa88fa069569d252fe2eb09618868a5f2540065c1e2380a95b77a74e3f334af2 |
|
MD5 | 5b33a737683a5804242f8fd26cfb44c5 |
|
BLAKE2b-256 | 60265404e25dab239eb7c76c8dcaad4cc73ce7b9fb0a46f86efe24119fec68fc |
Hashes for aiohttp-3.1.2-cp36-cp36m-macosx_10_12_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e3b9f55baa2f4fec2c28128f83fce65afeccca3ea34fda5b047f22b7c3d852ce |
|
MD5 | d6033cdbc9e2ecd2bcf152b2e37eaf8f |
|
BLAKE2b-256 | bbb96d67b7137ee2959de62b91c70b8124b6bc7e15b4e07508a3063d5b9851d8 |
Hashes for aiohttp-3.1.2-cp36-cp36m-macosx_10_11_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | caac911c1adfa8998f31860460a210cdc14f63c834a75514d8ccfb7b42356169 |
|
MD5 | fe609ce07c03ffdc4361dd506eecd848 |
|
BLAKE2b-256 | 28e3dfc74f37ffb7f1adf7f747f14421b783a2730b71a5b4467ef5ea6ef931a5 |
Hashes for aiohttp-3.1.2-cp36-cp36m-macosx_10_10_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f1c5f4429d1af999ffb9680158269e84ef1ea928cb538f406f411b064b9372d1 |
|
MD5 | 953f826aea16e33a9703abb3c30def56 |
|
BLAKE2b-256 | 8bf596800ecd82d08f1150ff05f5f4881106106eb551008ea1442d4ba6c5755f |
Hashes for aiohttp-3.1.2-cp35-cp35m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d8a28fa338637837c764f956728280d0df16145b118b51cbca10327209f800e0 |
|
MD5 | cc248212b1d6b9aae8fa75d7606dcaea |
|
BLAKE2b-256 | 0d5521c55d7aa95559bc3f790cf31b761761c4cc2763ec22acb01a5940a638ba |
Hashes for aiohttp-3.1.2-cp35-cp35m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | efa3f148891ec17630cd951ce76835d2158925dd22af0a1e5c20b929550a49f3 |
|
MD5 | abb03c39c9c131458350bbe909afd299 |
|
BLAKE2b-256 | 6d105b1633e5cd849ee87e1fd79e64e1af031c0e6a96d9eddd922072d0ebf93e |
Hashes for aiohttp-3.1.2-cp35-cp35m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 13dcc9f7d365a1224c3b4ba1f7d8ec5dfa78d263d3d317e0c40a79bdb01eda8f |
|
MD5 | fb2d8f75be073ed226bcdb68a99db231 |
|
BLAKE2b-256 | 449ae3d05a901ac14f317c63ac8a719038617ca6c723ea3a5855a38b278fd7a2 |
Hashes for aiohttp-3.1.2-cp35-cp35m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bfd396faa20a464c9b7d5973def3b912bf39d40bbc7a177402cf3fb086ac0c34 |
|
MD5 | 9837ed9fc7376eeaf07393a1ef236361 |
|
BLAKE2b-256 | 0970834a39aee571e726d68ea2c01d880bb958a5ff4aa2899cf45bc80993a6b0 |
Hashes for aiohttp-3.1.2-cp35-cp35m-macosx_10_12_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e82d04847504a6ad78442c950482756365115a34ecc4652a9d271f57ba76dbfb |
|
MD5 | 8b6ecaf31bf9baa922d1313101a8afc0 |
|
BLAKE2b-256 | c94db7cd21286ed3f38f77233a91a8ae347c916324922c031a14d9eefd044e38 |
Hashes for aiohttp-3.1.2-cp35-cp35m-macosx_10_11_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | efd24e6a55f6069994a5696232bc80a194d5a39da4518fdb63a4e91bc5c19d8d |
|
MD5 | 600cbdcec073af7106cafc8f3e4a73e6 |
|
BLAKE2b-256 | d88b2d7e5a2b825b76d5d22550d98ac23ac64ec36f08b407994f2ad908dc1186 |
Hashes for aiohttp-3.1.2-cp35-cp35m-macosx_10_10_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4f1d7344ec730333df454c71f0ec6e0f095d89afb5a744e230d7834cadc429dd |
|
MD5 | c4fb022784a17510fe68f3b04560f502 |
|
BLAKE2b-256 | f38d5f8e5a58a4487e119b2ccc8be4ca2ed4b81a8ee381c77303a7f908eed95e |