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 and avoids Callback Hell.
Provides Web-server with middlewares and pluggable routing.
Getting started
Client
To get something from the web:
import aiohttp
import asyncio
async def fetch(session, url):
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
An example using a simple server:
# examples/server_simple.py
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.WSMsgType.text:
await ws.send_str("Hello, {}".format(msg.data))
elif msg.type == web.WSMsgType.binary:
await ws.send_bytes(msg.data)
elif msg.type == web.WSMsgType.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
Demos
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 its 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 efficiency, the AsyncIO community maintains a list of benchmarks on the official wiki: https://github.com/python/asyncio/wiki/Benchmarks
Changelog
3.5.2 (2019-01-08)
Features
Bugfixes
Preserve MultipartWriter parts headers on write.
Refactor the way how Payload.headers are handled. Payload instances now always have headers and Content-Type defined.
Fix Payload Content-Disposition header reset after initial creation. #3035
Log suppressed exceptions in GunicornWebWorker. #3464
Remove wildcard imports. #3468
Use the same task for app initialization and web server handling in gunicorn workers. It allows to use Python3.7 context vars smoothly. #3471
Fix handling of chunked+gzipped response when first chunk does not give uncompressed data #3477
Replace collections.MutableMapping with collections.abc.MutableMapping to avoid a deprecation warning. #3480
Payload.size type annotation changed from Optional[float] to Optional[int]. #3484
Ignore done tasks when cancels pending activities on web.run_app finalization. #3497
Improved Documentation
Add documentation for aiohttp.web.HTTPException. #3490
Misc
3.5.1 (2018-12-24)
Fix a regression about ClientSession._requote_redirect_url modification in debug mode.
3.5.0 (2018-12-22)
Features
The library type annotations are checked in strict mode now.
Add support for setting cookies for individual request (#2387)
Application.add_domain implementation (#2809)
The default app in the request returned by test_utils.make_mocked_request can now have objects assigned to it and retrieved using the [] operator. (#3174)
Make request.url accessible when transport is closed. (#3177)
Add zlib_executor_size argument to Response constructor to allow compression to run in a background executor to avoid blocking the main thread and potentially triggering health check failures. (#3205)
Enable users to set ClientTimeout in aiohttp.request (#3213)
Don’t raise a warning if NETRC environment variable is not set and ~/.netrc file doesn’t exist. (#3267)
Add default logging handler to web.run_app
If the Application.debug flag is set and the default logger aiohttp.access is used, access logs will now be output using a stderr StreamHandler if no handlers are attached. Furthermore, if the default logger has no log level set, the log level will be set to DEBUG. (#3324)
Add method argument to session.ws_connect().
Sometimes server API requires a different HTTP method for WebSocket connection establishment.
For example, Docker exec needs POST. (#3378)
Create a task per request handling. (#3406)
Bugfixes
Enable passing access_log_class via handler_args (#3158)
Return empty bytes with end-of-chunk marker in empty stream reader. (#3186)
Accept CIMultiDictProxy instances for headers argument in web.Response constructor. (#3207)
Don’t uppercase HTTP method in parser (#3233)
Make method match regexp RFC-7230 compliant (#3235)
Add app.pre_frozen state to properly handle startup signals in sub-applications. (#3237)
Enhanced parsing and validation of helpers.BasicAuth.decode. (#3239)
Change imports from collections module in preparation for 3.8. (#3258)
Ensure Host header is added first to ClientRequest to better replicate browser (#3265)
Fix forward compatibility with Python 3.8: importing ABCs directly from the collections module will not be supported anymore. (#3273)
Keep the query string by normalize_path_middleware. (#3278)
Fix missing parameter raise_for_status for aiohttp.request() (#3290)
Bracket IPv6 addresses in the HOST header (#3304)
Fix default message for server ping and pong frames. (#3308)
Fix tests/test_connector.py typo and tests/autobahn/server.py duplicate loop def. (#3337)
Fix false-negative indicator end_of_HTTP_chunk in StreamReader.readchunk function (#3361)
Release HTTP response before raising status exception (#3364)
Fix task cancellation when sendfile() syscall is used by static file handling. (#3383)
Fix stack trace for asyncio.TimeoutError which was not logged, when it is caught in the handler. (#3414)
Improved Documentation
Deprecations and Removals
Deprecate modification of session.requote_redirect_url (#2278)
Deprecate stream.unread_data() (#3260)
Deprecated use of boolean in resp.enable_compression() (#3318)
Encourage creation of aiohttp public objects inside a coroutine (#3331)
Drop dead Connection.detach() and Connection.writer. Both methods were broken for more than 2 years. (#3358)
Deprecate app.loop, request.loop, client.loop and connector.loop properties. (#3374)
Deprecate explicit debug argument. Use asyncio debug mode instead. (#3381)
Deprecate body parameter in HTTPException (and derived classes) constructor. (#3385)
Deprecate bare connector close, use async with connector: and await connector.close() instead. (#3417)
Deprecate obsolete read_timeout and conn_timeout in ClientSession constructor. (#3438)
Misc
#3341, #3351
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.5.2-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d85ae8281d43016f4c6d9bee93f13b43933c405784ff3e0d68ff31b45290010a |
|
MD5 | 42da0be559f3c974ab7c835ccd00bba7 |
|
BLAKE2b-256 | 485400a4ce67a3fcc723b2d634ec1ae3bd798e654aaa8e7a30116b7c9b7c7142 |
Hashes for aiohttp-3.5.2-cp37-cp37m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a2d86fda9b71a0a47f8fa8910322ca149cb9aa18f1649d7a625f97f0911c3bdc |
|
MD5 | 985d135bd9d35ad3ce3dc92566edc64d |
|
BLAKE2b-256 | dc364a535af6aaf5067e772f87de7bb82cc9b8d7386b4fdb6686be08b55cd898 |
Hashes for aiohttp-3.5.2-cp37-cp37m-macosx_10_13_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e74a93b56bda8c5d4b1de53f4244b18995302b955d44aab6e9f422676f1febe2 |
|
MD5 | 401b44f7273913e696db5d274dadb81a |
|
BLAKE2b-256 | c155ce987a700fbd997975e1ccf8450789ed645f530c9614dba0f7aea6a0fc28 |
Hashes for aiohttp-3.5.2-cp37-cp37m-macosx_10_11_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ce4ed6df667e5add49362af1e1d7a38d511cebb717caa8f2e97adfdb1a730402 |
|
MD5 | 6f7ecc3eb4abbc8000a87c40162e1a4a |
|
BLAKE2b-256 | e7b792645b962db0a23c61438e9aa16cab384d27b4ec5ceed46eaccde89b94ef |
Hashes for aiohttp-3.5.2-cp37-cp37m-macosx_10_10_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3a9799ca609a27dbe74762f2be1267695464218614fa20d9b19bae298716c56a |
|
MD5 | 5cde92dadaa6fb40bdb34056060bab47 |
|
BLAKE2b-256 | c48af8ffb0fe5e8fcfb545f03bc1b93448eaa9bdf0ae5658e35438bec4351dd2 |
Hashes for aiohttp-3.5.2-cp36-cp36m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f9847ad31aec2ba4697b28ca50dc933b14fedc97e1090797e819be30180d62c4 |
|
MD5 | 9880bd0d3380b6cbdf241fe64cdabf8f |
|
BLAKE2b-256 | 80399a269c7e8be4b9d39bc49c9a374bd17b64e1d610442e3f1fcc200edcd461 |
Hashes for aiohttp-3.5.2-cp36-cp36m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c86549a6cea53442e1bbfbae3415e49220fcbd181405b5fae54c1ca2f0285347 |
|
MD5 | cf75cb68c10aef4b87057f7483d2ce7a |
|
BLAKE2b-256 | 8a2a5d0e92fc9ae92e3adddf46474bdfe48d882ff637f18c7a7b5a2984060233 |
Hashes for aiohttp-3.5.2-cp36-cp36m-macosx_10_13_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bf9a2c14c3ef664e7c04684fbe1ddbefc20966636fc659ab0189cb00475d3149 |
|
MD5 | 2375075c92e48384b619591ea36165e2 |
|
BLAKE2b-256 | 89e3bf2d53f4ad854b7d837bf1faa49b1a2c11ed45f517b45b1c4f236f72b5bb |
Hashes for aiohttp-3.5.2-cp36-cp36m-macosx_10_11_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 17e29040b80c0674696c806ba86cba6e68813966825885027e2c39054f51ff0b |
|
MD5 | cdd3087737238f29e43ac4ab6b9a0994 |
|
BLAKE2b-256 | 11d268cedaabe9b9577076ad90298de2427dd8891d9e6c3c60886abcca255c4d |
Hashes for aiohttp-3.5.2-cp36-cp36m-macosx_10_10_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7db10a247c164966c4755098dcf53b999cbe2709af0004ddb7a9af20a64cc544 |
|
MD5 | 0efc25929d1872810d49d478947865c3 |
|
BLAKE2b-256 | a69ac97c63704a480a56c941a3a48798f784db8add4470bfa94a9a2d102c7a6f |
Hashes for aiohttp-3.5.2-cp35-cp35m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 88f05b3eb290588b4ec250be92c7f1d61c3773a965a053f0ebb918cec2fea99d |
|
MD5 | 79cf9a1b2463b7f047e55f5a9cdc48ff |
|
BLAKE2b-256 | 451e948b559e69b20b9b924a0acb1e777e71abc45b061e146cc48adb139fbbb4 |
Hashes for aiohttp-3.5.2-cp35-cp35m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ccc106721cbc2cb3a436892677a71611214220aed9b14e55b1ef8a053be0d5c2 |
|
MD5 | 18abf3e877de0de042c4ecb58601ec42 |
|
BLAKE2b-256 | a47df91d882459b2fddfd61538f305d5cec93888a97b0fe93e33b43021ae836d |
Hashes for aiohttp-3.5.2-cp35-cp35m-macosx_10_13_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4c909075fdbb72d32cf4264d3d7636b34bc593bcd6e91ebc6a09614f6a358a9f |
|
MD5 | 26fcc8c796a9465ecc9c058d75ddce15 |
|
BLAKE2b-256 | a9a1046e97ab236e15f98e877aa678751a038f78e5d4d1f797ee6b99aca07d87 |
Hashes for aiohttp-3.5.2-cp35-cp35m-macosx_10_11_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3c29653e39dba10e9fd309ca55a429c8ae872ec2d0383a7124fad32ddca7f68d |
|
MD5 | cefbe8ba35d27b084753d14b32476108 |
|
BLAKE2b-256 | 7283884bfbc95c0ad7484a40ffa7b0bb9089c0a1fdedb0c6da890bfd6bd2b072 |
Hashes for aiohttp-3.5.2-cp35-cp35m-macosx_10_10_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b4697481137b93df776921af6f004006f9983287d0ec1de1fef5cc5003de5e0f |
|
MD5 | f68a89ad43730b34a19b85c2888e5da2 |
|
BLAKE2b-256 | 9d9ec25ba40bf9014b77c33d81456c2d8257b1468f2fb777f6252b7c55800b11 |