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.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.0-cp36-cp36m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | feb57395cacf87cc4b621d4b4cf8eb2dfb0dc97b47e7443d2b3349dff4c3769e |
|
MD5 | dbc2cf6e2444fd60ec9b2fc5234347d8 |
|
BLAKE2b-256 | 27a5c40c5f65f737dc643d329047445217ecde65ee755dfe12d89b298adb3f50 |
Hashes for aiohttp-3.1.0-cp36-cp36m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c0c12ca08d97e5a19a4e65573eaee70d3d371f2f1a8059e5229684d3eef1d10c |
|
MD5 | fd1c50edd12437b358d0af75ce56a212 |
|
BLAKE2b-256 | f2f9ae4d4239d64ecfeadc453ad62f3d5a79d72b41f3e8bb1d60cc176bae7381 |
Hashes for aiohttp-3.1.0-cp36-cp36m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4f94b2b9e70a2e91e7ba2caf05c21251741af9db9c837e5ec874042bef34879a |
|
MD5 | e8a9e2bcb107d55edf36d5c922eee1e6 |
|
BLAKE2b-256 | eff4cbca4466fcabd8c8e41c00a0d135288ce59ebfa35707614e4e7f1eefbcc0 |
Hashes for aiohttp-3.1.0-cp36-cp36m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8d87c610dc8c762e2f9e5fbe127fd0e0e850cf36141db939788558a225596506 |
|
MD5 | e20ba7bbe07385193880189c0108f6cf |
|
BLAKE2b-256 | 8e58cd0748b9a8e645d9af905254dc1729289b869a82137ddab592b0192ebc55 |
Hashes for aiohttp-3.1.0-cp36-cp36m-macosx_10_12_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 73beb2efcce172af584ba233c4448f6860947aa81a4db8867bb8211124859777 |
|
MD5 | d58ea85d964a2982a890a0c3b5e46127 |
|
BLAKE2b-256 | 5e18e0db803fc886e31a2c5e94bdb3072235cb626f5f14da8d20dba279b7fc33 |
Hashes for aiohttp-3.1.0-cp36-cp36m-macosx_10_11_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b27f5f1d93dc05e25b3dc315ab76c7bd106fd0d58d4bb7ed0256e3a538300e65 |
|
MD5 | 959672c4e7c4e7e7e614aed381361d96 |
|
BLAKE2b-256 | 30ce2c0810038fe33040dc666196989cac65f9d7fa56120169ce2707e1167f37 |
Hashes for aiohttp-3.1.0-cp36-cp36m-macosx_10_10_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b99c8286770bbec5f226786d453a4e5602a052843f0865735b22ac6be5c20972 |
|
MD5 | 35f433a23f9acb8bab7870cc507fa351 |
|
BLAKE2b-256 | d2602f637741b112b03560e98c236e00f3afba943b19871272652bd1c5f7d06e |
Hashes for aiohttp-3.1.0-cp35-cp35m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3f197bcdc0a801ecfa48d2ed0a0a49842668da1cbec25745cf65da68f7e62069 |
|
MD5 | d82af1655326d3ba830aa96b4abc7459 |
|
BLAKE2b-256 | ed584f6b2beada2b9b6536ad9bb3b35f18b5f6083c5dea9714e01b841d2ebbc3 |
Hashes for aiohttp-3.1.0-cp35-cp35m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d69ead4d1a9ad02c4164cb3ff75b6e95793540517f9085430d25d521f50bcd1c |
|
MD5 | 2a4ed6d0c9d8c10829897abd075c40ea |
|
BLAKE2b-256 | cfe8a6ccd2b844e214dec1a269d8167ccc84b8dc8bc3a3aeb849ed6b8f05cbf0 |
Hashes for aiohttp-3.1.0-cp35-cp35m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8fe4cb34b734377d1e64070a7eea50a3776dd9478f91722f4dbb1a57ff9e6d99 |
|
MD5 | f6371394e7c613216a360a357a4dd015 |
|
BLAKE2b-256 | ca725b9516d2d94b0a28c465c84b098d7febcac1b446f5ded68d71a9845aad3a |
Hashes for aiohttp-3.1.0-cp35-cp35m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9c68046dd6f986619942149b87e13e6be5272fbcb268d791dc8f2015eb9e30cc |
|
MD5 | c4d5cb71ee964e28432d194c4f956d06 |
|
BLAKE2b-256 | 971fe537cf73ff41835404d53624f9b749cab766f1ee54a0d2d8f5d75b19d8c4 |
Hashes for aiohttp-3.1.0-cp35-cp35m-macosx_10_12_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 04ec521058055b2501b5c31419c8bd6212e10785653834838981fcd234402364 |
|
MD5 | 6b99eff00107a79e5416face325d8867 |
|
BLAKE2b-256 | 4b066722b1a0a9bcac743012cf92379268111930e0f89834d2db52bdb9d32330 |
Hashes for aiohttp-3.1.0-cp35-cp35m-macosx_10_11_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a66c36fd969336278ccb8398c891e268e9fb2ee9e5866433fa4e0b7436d747e7 |
|
MD5 | e08d5fa5dbaf3e463b89a72bb2ce1338 |
|
BLAKE2b-256 | 228fc93d35493fb58cf058d774ae41611ef3fb242136bb244afe8c5c411a0136 |
Hashes for aiohttp-3.1.0-cp35-cp35m-macosx_10_10_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7e74f2f0c5c8c9da14ffd224dbdcef0b315201e3986f5b9961ea30f6d1754b1f |
|
MD5 | 731a44eda0bf13e1c5007259afb9c82c |
|
BLAKE2b-256 | bca7024776d9f7e756eced07b64cab49a1f7ce7ae1e9c0cbaea7ad8c4d59b543 |