http client/server for asyncio
Project description
http client/server for asyncio
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 async def fetch(session, url): with aiohttp.Timeout(10, loop=session.loop): async with session.get(url) as response: return await response.text() async def main(loop): async with aiohttp.ClientSession(loop=loop) as session: html = await fetch(session, 'http://python.org') print(html) if __name__ == '__main__': loop = asyncio.get_event_loop() loop.run_until_complete(main(loop))
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: ws.send_str("Hello, {}".format(msg.data)) elif msg.type == web.MsgType.binary: 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)
Note: examples are written for Python 3.5+ and utilize PEP-492 aka async/await. If you are using Python 3.4 please replace await with yield from and async def with @coroutine e.g.:
async def coro(...): ret = await f()
should be replaced by:
@asyncio.coroutine def coro(...): ret = yield from f()
Documentation
Discussion list
aio-libs google group: https://groups.google.com/forum/#!forum/aio-libs
Requirements
- Python >= 3.4.2
- async-timeout
- chardet
- multidict
- yarl
Optionally you may install the cChardet and aiodns libraries (highly recommended for sake of speed).
License
aiohttp is offered under the Apache 2 license.
Source code
The latest developer version is available in a github repository: https://github.com/KeepSafe/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
CHANGES
1.3.1 (2017-02-09)
- Handle CLOSING in WebSocketResponse.__anext__
- Fixed AttributeError ‘drain’ for server websocket handler #1613
1.3.0 (2017-02-08)
- Multipart writer validates the data on append instead of on a request send #920
- Multipart reader accepts multipart messages with or without their epilogue to consistently handle valid and legacy behaviors #1526 #1581
- Separate read + connect + request timeouts # 1523
- Do not swallow Upgrade header #1587
- Fix polls demo run application #1487
- Ignore unknown 1XX status codes in client #1353
- Fix sub-Multipart messages missing their headers on serialization #1525
- Do not use readline when reading the content of a part in the multipart reader #1535
- Add optional flag for quoting FormData fields #916
- 416 Range Not Satisfiable if requested range end > file size #1588
- Having a : or @ in a route does not work #1552
- Added receive_timeout timeout for websocket to receive complete message. #1325
- Added heartbeat parameter for websocket to automatically send ping message. #1024 #777
- Remove web.Application dependency from web.UrlDispatcher #1510
- Accepting back-pressure from slow websocket clients #1367
- Do not pause transport during set_parser stage #1211
- Lingering close doesn’t terminate before timeout #1559
- setsockopt may raise OSError exception if socket is closed already #1595
- Lots of CancelledError when requests are interrupted #1565
- Allow users to specify what should happen to decoding errors when calling a responses text() method #1542
- Back port std module http.cookies for python3.4.2 #1566
- Maintain url’s fragment in client response #1314
- Allow concurrently close WebSocket connection #754
- Gzipped responses with empty body raises ContentEncodingError #609
- Return 504 if request handle raises TimeoutError.
- Refactor how we use keep-alive and close lingering timeouts.
- Close response connection if we can not consume whole http message during client response release
- Abort closed ssl client transports, broken servers can keep socket open un-limit time #1568
- Log warning instead of RuntimeError is websocket connection is closed.
- Deprecated: aiohttp.protocol.HttpPrefixParser will be removed in 1.4 #1590
- Deprecated: Servers response’s .started, .start() and .can_start() method will be removed in 1.4 #1591
- Deprecated: Adding sub app via app.router.add_subapp() is deprecated use app.add_subapp() instead, will be removed in 1.4 #1592
- Deprecated: aiohttp.get(), aiohttp.options(), aiohttp.head(), aiohttp.post(), aiohttp.put(), aiohttp.patch(), aiohttp.delete(), and aiohttp.ws_connect() will be removed in 1.4 #1593
- Deprecated: Application.finish() and Application.register_on_finish() will be removed in 1.4 #1602
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-1.3.1-cp36-cp36m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 13f44a971da01093a5abcc6436596558e0c5bfac2532407a097be8607510ee1a |
|
MD5 | f6de3c4efc3c52629970f870aded1bec |
|
BLAKE2-256 | 3ac149a013795a1369815673263b3c17b26d761c8d4f4cd399d8e0cd22c52775 |
Hashes for aiohttp-1.3.1-cp36-cp36m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8e71993f16870ebb1e8af21cb65dbac0cb6418ce9ab5a8cfaefce1c986a306bf |
|
MD5 | 26dd55be4c7cbf56849738c4a8ca10e4 |
|
BLAKE2-256 | a401569a73217dc2367e604979eee20f67dd94f929651c4227445ff08712ccf6 |
Hashes for aiohttp-1.3.1-cp35-cp35m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bdd4b816dd2fdda95ca78170c1c2b5763325278ece8e77a6127e1dd4496b6b23 |
|
MD5 | 342fafe416873f1607a63bd3087c7ee4 |
|
BLAKE2-256 | 0d032336cb25efb4b5171731aa6f02ef4a9048785356a362d3d0eb8fd69a7f55 |
Hashes for aiohttp-1.3.1-cp35-cp35m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0e8f441d06f6236ceab2f46ba9df0a61e2353b267f23bb74bf2d8e8e2f7df35a |
|
MD5 | 8e6cc2d442b84c3401e12da96216410c |
|
BLAKE2-256 | 14527780aaeeee1a5adc0a574c706075ded04dded392f22b23536d3451ef5345 |
Hashes for aiohttp-1.3.1-cp35-cp35m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 52aa64ae3fa7b9681805ef321d27dd129e1e0c083646c693e688bed46fd141b6 |
|
MD5 | df15a56c4a568302ed833200eb55b00b |
|
BLAKE2-256 | c63ba38626ed0bc5514baecf0e8843492720e2974969c877c4e668f4f0936f52 |
Hashes for aiohttp-1.3.1-cp35-cp35m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0977a790c34b1c199df17d7205033c89a55c287495da5aff3af813f1d312bc4f |
|
MD5 | 3d1048b80ab29df8b039e94d64d0d79d |
|
BLAKE2-256 | fd2443e57d1d5c463c4b7f03b05fde0b85278ca5a186d01ed20fffac723cdc08 |
Hashes for aiohttp-1.3.1-cp34-cp34m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 54ea9e6d43e8446a17ede03f80c17885899d0284cbab3cc273958e6088c75429 |
|
MD5 | 2a6deaf0d62c63e09e697a7fb72e6e7d |
|
BLAKE2-256 | 31a21b493ebfc4b4a56600a37eb7004cf8ae3bbe69ce781055a41bc0df4cf7ec |
Hashes for aiohttp-1.3.1-cp34-cp34m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 01f57f73ae8b448ed0f48b0bc479047429d81adc8b96106b955028decba0943c |
|
MD5 | 40a6e1e5f6c6940f1aa1ea42a5edeaf3 |
|
BLAKE2-256 | 5e7d9a8b886a6d0a87afd2858fa88efbf6f50cda26346242776b8e67cccd1a64 |
Hashes for aiohttp-1.3.1-cp34-cp34m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8e79e85793bcbf5df04555b90a9a1be71a6b74e5a6b3aa85eaf7f8de6c97faaf |
|
MD5 | 05c0b20294ac78d1f46dcff286552d10 |
|
BLAKE2-256 | bdb8f5b7fd5dd7b007be9f7de84fe46608d9e8dc40bd06e1833ef781b5bbb258 |
Hashes for aiohttp-1.3.1-cp34-cp34m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a7c1ea6d0bae3b959e5fd219bc4900c08f2bd55157b26ac634e610704376fc3c |
|
MD5 | 6fc677e8479f5dfb8c9dd2535a845090 |
|
BLAKE2-256 | 641e2cdfd081ab3a133dadcdc7cf3dcb6815843314421c059925e362d903e5b5 |