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.
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 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)
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.0.7 (2018-03-08)
Fix SSL proxy support by client. (#2810)
Restore a imperative check in setup.py for python version. The check works in parallel to environment marker. As effect a error about unsupported Python versions is raised even on outdated systems with very old setuptools version installed. (#2813)
3.0.6 (2018-03-05)
Add _reuse_address and _reuse_port to web_runner.TCPSite.__slots__. (#2792)
3.0.5 (2018-02-27)
Fix InvalidStateError on processing a sequence of two RequestHandler.data_received calls on web server. (#2773)
3.0.4 (2018-02-26)
Fix IndexError in HTTP request handling by server. (#2752)
Fix MultipartWriter.append* no longer returning part/payload. (#2759)
3.0.3 (2018-02-25)
Relax attrs dependency to minimal actually supported version 17.0.3 The change allows to avoid version conflicts with currently existing test tools.
3.0.2 (2018-02-23)
Security Fix
Prevent Windows absolute URLs in static files. Paths like /static/D:\path and /static/\\hostname\drive\path are forbidden.
3.0.1
Technical release for fixing distribution problems.
3.0.0 (2018-02-12)
Features
Speed up the PayloadWriter.write method for large request bodies. (#2126)
StreamResponse and Response are now MutableMappings. (#2246)
ClientSession publishes a set of signals to track the HTTP request execution. (#2313)
Content-Disposition fast access in ClientResponse (#2455)
Added support to Flask-style decorators with class-based Views. (#2472)
Signal handlers (registered callbacks) should be coroutines. (#2480)
Support async with test_client.ws_connect(...) (#2525)
Introduce site and application runner as underlying API for web.run_app implementation. (#2530)
Only quote multipart boundary when necessary and sanitize input (#2544)
Make the aiohttp.ClientResponse.get_encoding method public with the processing of invalid charset while detecting content encoding. (#2549)
Add optional configurable per message compression for ClientWebSocketResponse and WebSocketResponse. (#2551)
Add hysteresis to StreamReader to prevent flipping between paused and resumed states too often. (#2555)
Support .netrc by trust_env (#2581)
Avoid to create a new resource when adding a route with the same name and path of the last added resource (#2586)
MultipartWriter.boundary is str now. (#2589)
Allow a custom port to be used by TestServer (and associated pytest fixtures) (#2613)
Add param access_log_class to web.run_app function (#2615)
Add ssl parameter to client API (#2626)
Fixes performance issue introduced by #2577. When there are no middlewares installed by the user, no additional and useless code is executed. (#2629)
Rename PayloadWriter to StreamWriter (#2654)
New options reuse_port, reuse_address are added to run_app and TCPSite. (#2679)
Use custom classes to pass client signals parameters (#2686)
Use attrs library for data classes, replace namedtuple. (#2690)
Pytest fixtures renaming, add aiohttp_ prefix (#2578)
Add aiohttp- prefix for pytest-aiohttp command line parameters (#2578)
Bugfixes
Correctly process upgrade request from server to HTTP2. aiohttp does not support HTTP2 yet, the protocol is not upgraded but response is handled correctly. (#2277)
Fix ClientConnectorSSLError and ClientProxyConnectionError for proxy connector (#2408)
Fix connector convert OSError to ClientConnectorError (#2423)
Fix connection attempts for multiple dns hosts (#2424)
Fix writing to closed transport by raising asyncio.CancelledError (#2499)
Fix warning in ClientSession.__del__ by stopping to try to close it. (#2523)
Fixed race-condition for iterating addresses from the DNSCache. (#2620)
Fix default value of access_log_format argument in web.run_app (#2649)
Freeze sub-application on adding to parent app (#2656)
Do percent encoding for .url_for() parameters (#2668)
Correctly process request start time and multiple request/response headers in access log extra (#2641)
Improved Documentation
Improve tutorial docs, using literalinclude to link to the actual files. (#2396)
Small improvement docs: better example for file uploads. (#2401)
Rename from_env to trust_env in client reference. (#2451)
Fixed mistype in Proxy Support section where trust_env parameter was used in session.get(“http://python.org”, trust_env=True) method instead of aiohttp.ClientSession constructor as follows: aiohttp.ClientSession(trust_env=True). (#2688)
Fix issue with unittest example not compiling in testing docs. (#2717)
Deprecations and Removals
Simplify HTTP pipelining implementation (#2109)
Drop StreamReaderPayload and DataQueuePayload. (#2257)
Drop md5 and sha1 finger-prints (#2267)
Drop WSMessage.tp (#2321)
Drop Python 3.4 and Python 3.5.0, 3.5.1, 3.5.2. Minimal supported Python versions are 3.5.3 and 3.6.0. yield from is gone, use async/await syntax. (#2343)
Drop aiohttp.Timeout and use async_timeout.timeout instead. (#2348)
Drop resolve param from TCPConnector. (#2377)
Add DeprecationWarning for returning HTTPException (#2415)
send_str(), send_bytes(), send_json(), ping() and pong() are genuine async functions now. (#2475)
Drop undocumented app.on_pre_signal and app.on_post_signal. Signal handlers should be coroutines, support for regular functions is dropped. (#2480)
StreamResponse.drain() is not a part of public API anymore, just use await StreamResponse.write(). StreamResponse.write is converted to async function. (#2483)
Drop deprecated slow_request_timeout param and **kwargs` from RequestHandler. (#2500)
Drop deprecated resource.url(). (#2501)
Remove %u and %l format specifiers from access log format. (#2506)
Drop deprecated request.GET property. (#2547)
Simplify stream classes: drop ChunksQueue and FlowControlChunksQueue, merge FlowControlStreamReader functionality into StreamReader, drop FlowControlStreamReader name. (#2555)
Do not create a new resource on router.add_get(…, allow_head=True) (#2585)
Drop access to TCP tuning options from PayloadWriter and Response classes (#2604)
Drop deprecated encoding parameter from client API (#2606)
Deprecate verify_ssl, ssl_context and fingerprint parameters in client API (#2626)
Get rid of the legacy class StreamWriter. (#2651)
Forbid non-strings in resource.url_for() parameters. (#2668)
Deprecate inheritance from ClientSession and web.Application and custom user attributes for ClientSession, web.Request and web.Application (#2691)
Drop resp = await aiohttp.request(…) syntax for sake of async with aiohttp.request(…) as resp:. (#2540)
Forbid synchronous context managers for ClientSession and test server/client. (#2362)
Misc
#2552
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.0.7-cp36-cp36m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9a636c99157248d4befb35bf6b4b2c423897269229c45f03578edc34a7c36c3f |
|
MD5 | 9bbd6cdc9a402c247aa0aeccf6da4a72 |
|
BLAKE2b-256 | d1cbb649cb41c4d213c9d8b2b7afcfd0bd34ec6fdeb298592f0c6d9664c96a53 |
Hashes for aiohttp-3.0.7-cp36-cp36m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c34d51fbc3210f781a82dd4314ca385aa07387ccb72fe294a4989d5ea629efba |
|
MD5 | 443044aa4e0d2bc6b32bfef5444d93c2 |
|
BLAKE2b-256 | f6da0a79428cb7d7fcaf264782027bbfb36cf86ae73a1df1897376e32b133be1 |
Hashes for aiohttp-3.0.7-cp36-cp36m-macosx_10_12_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 226d14ed9e9417a7bf0f5ce943ac907d441786e2dadb28b9501fb6913e7e7e57 |
|
MD5 | 7be6e1fa8e13b934efedb1a34efc08f4 |
|
BLAKE2b-256 | 8bbb1164f94f90d3d91de5b938250e8a462b2be9cd2dc0d987eefe410318fb03 |
Hashes for aiohttp-3.0.7-cp36-cp36m-macosx_10_11_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 41149c06057fed6498d4b5892c3492be87c66b66eda609b26ae0fb941e361858 |
|
MD5 | d0732fc57d31c56c100540e7171ffedc |
|
BLAKE2b-256 | a3e2f6e473a1d71505e4206f1c668bac6e5136d7781574fb4038cb4cea6c3952 |
Hashes for aiohttp-3.0.7-cp36-cp36m-macosx_10_10_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7207c758787acbd6a248f119e1b3002473b1cc6f9a15f3669dd393620dead46d |
|
MD5 | 74e5c6d411bd214aa307d6a78cb14ead |
|
BLAKE2b-256 | e62ce589645db1c734f11611394c4d04372fbba84619943bb56bd9ddde9c8277 |
Hashes for aiohttp-3.0.7-cp35-cp35m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 97b891eb7d4db37f90cd8052fc97fabdb21432233463df69df389903ca5dd18f |
|
MD5 | 56fd4c3796a0d06e5f4518b8c863480b |
|
BLAKE2b-256 | 611e8311436b09a75b8c45583d147e7e73075baf2ba3c36dfe47ea23dd384b57 |
Hashes for aiohttp-3.0.7-cp35-cp35m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f1d3d9d959aeb4b1938786e5f911bb1be6559353e8eaa117e2ad64270b9d2c9c |
|
MD5 | 48384d42281d6f65218fa3da53b84e09 |
|
BLAKE2b-256 | 87755285b26f379ec10ec5bb86de40f9ad6edc6798f3b588d48d633fcbf9a75e |
Hashes for aiohttp-3.0.7-cp35-cp35m-macosx_10_12_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2eab0797219f8fa3f20bcc6c958e926c7e5d0ab21dd4f995fd3d7c758eca4f52 |
|
MD5 | 7cb24385e14806e68b3df1d760505030 |
|
BLAKE2b-256 | 7c7ee6ae29a2d92fdd5d4c455216f40a69d331f03bb25b3fbd2dc9319580412b |
Hashes for aiohttp-3.0.7-cp35-cp35m-macosx_10_11_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a126abc5f8f3bcc3e95e12872a77054f501884035d8eecc64621ae61655daa87 |
|
MD5 | 22022ce30026c560e109ea4bbeff6aad |
|
BLAKE2b-256 | 83aea155a0ebab8fa71eece082765a6c3b3ac7d079c85b67131c20b154aa105f |
Hashes for aiohttp-3.0.7-cp35-cp35m-macosx_10_10_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 96adfed345c13499eb6af47d274bfe9ed164f3a6ad269c8635b5316c15a97b43 |
|
MD5 | 78e0660a5f2a333eb4c1a0e2947f2842 |
|
BLAKE2b-256 | 1cc12a940a807a071261c6cf27a58c7beb22ae24a66328fb7411de50b8c902ac |