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: 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)
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.
Keepsafe
The aiohttp community would like to thank Keepsafe (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
CHANGES
1.3.4 (2017-03-14)
- Revert timeout handling in client request
- Fix StreamResponse representation after eof
- Fix file_sender to not fall on bad request (range out of file size)
- Fix file_sender to correct stream video to Chromes
- Fix NotImplementedError server exception #1703
- Clearer error message for URL without a host name. #1691
- Silence deprecation warning in __repr__ #1690
- IDN + HTTPS = ssl.CertificateError #1685
1.3.3 (2017-02-19)
- Fixed memory leak in time service #1656
1.3.2 (2017-02-16)
- Awaiting on WebSocketResponse.send_* does not work #1645
- Fix multiple calls to client ws_connect when using a shared header dict #1643
- Make CookieJar.filter_cookies() accept plain string parameter. #1636
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.4-cp36-cp36m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bf7eef8814fbba31ef61561b6f5ff23260f5bc8551a8eceac14421b0e044751a |
|
MD5 | 77a1f52d1da4c3cab058003207daef4a |
|
BLAKE2-256 | 0a2334c4dd0a3157ab21d0241205e58370ef4d14e75dc24a9b51066493b83b58 |
Hashes for aiohttp-1.3.4-cp36-cp36m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | febfb20b171203dd7b523890008484bf0b29d9354000143ab7dd6292a22c642f |
|
MD5 | be511a7f8b230f80398b0b1308f690d0 |
|
BLAKE2-256 | 3208d0bc8541eaf3105a6fa1e7417d24393c5d48397f8cbf41b41412348ce890 |
Hashes for aiohttp-1.3.4-cp35-cp35m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c404009edf24766e2bf31b4668eaeb3dd05f14832a161dd809efae24d084a01b |
|
MD5 | 2f95acd8dc25a25d8f0ccc8c60d0e2f4 |
|
BLAKE2-256 | c54745e641d56d96bf0b544c76efa9086c1f85af847f55e1daf7e0df4f5c68fc |
Hashes for aiohttp-1.3.4-cp35-cp35m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 89ca62ed76ce119393fc64b84e8ca148558d04db300542255f9f57af0e8a7237 |
|
MD5 | 46bc34c3c16363d6913c7002db354e67 |
|
BLAKE2-256 | 13a46941dbbee91db89825603f6449f1928e2047a898052d67449dcf1f553871 |
Hashes for aiohttp-1.3.4-cp34-cp34m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9f32e32aeb39418a94606952736929013f3672e76302b2ce3b43eb4c5c785aa9 |
|
MD5 | 42775078d500f5d760f0dd2e9fe7cfe6 |
|
BLAKE2-256 | a0b14e9a56a7387e03f7b8e13ea1412a8aeab19d5385410770eaa1e2d1c8abdc |
Hashes for aiohttp-1.3.4-cp34-cp34m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f6fa8ec31add46a5e7ef39edf120a32959e8c999ddb38e03dd09c5c3cdf5a8c3 |
|
MD5 | 86b5db5fdad3af6588e8acc5bc41c4df |
|
BLAKE2-256 | acb756b1ffbca63fb7e0ea33c0a7da593c5b11cc2668c6ec9f35ce916d4ee45e |