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.
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.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.2-cp36-cp36m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a5f78266881e53a4b84c49b3a1947c92080043ed5dde037c0a487539b2b346c1 |
|
MD5 | 1174f9bf8ae48e5c49b91715251730ea |
|
BLAKE2-256 | 2653eaa657ddbeacf8bffbba00855948fa8c23bfe5e67ff3c15cc5124160cf2f |
Hashes for aiohttp-1.3.2-cp36-cp36m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f11f2829f740b351102a0557fecc7911e0d174fd8c61f39501a1e9936a6370e3 |
|
MD5 | 2deefd099ac7af431d6c75964d1e56ae |
|
BLAKE2-256 | f1cdf4f41cf5c00a272e277c3c59919ca595a87dc1561fec89c31c1c14a0871a |
Hashes for aiohttp-1.3.2-cp36-cp36m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fb32c587e01cff2ae591e99e5e47a43e374a7ff4f191ee6a9e308561e9a625bc |
|
MD5 | c31db3f972ce00e45b2edf87802d0a46 |
|
BLAKE2-256 | d751d9c607f8af73ae6a47117e74f2b0ab00a12c934ad07121f4793e75af184f |
Hashes for aiohttp-1.3.2-cp36-cp36m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 39fefae0f4e0c4edfb2de6d8fc4e153801cb5577af268a69ea2a5846acafa720 |
|
MD5 | 7f26c29f37ff7eff6dd98719d5278572 |
|
BLAKE2-256 | 310c561b37a7db207aac7677dd4ff1af34df4211dceb4a6d1bad8e7ce207443d |
Hashes for aiohttp-1.3.2-cp35-cp35m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fef465facb975dfd360b40a7d22cadce619ba4975ddf1722b48a143e3f94fefe |
|
MD5 | 1390eadb4611938306110d84f662351f |
|
BLAKE2-256 | 130825d553129c83e9f2e5bc07d9fd345d43b0fd8a354bbf2f0bdda6dd189eff |
Hashes for aiohttp-1.3.2-cp35-cp35m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 79821d32608a286f776e438a42e5f1471ccf46d50b59a062d91e304fbc538654 |
|
MD5 | 8f76f67e952e0254fbe5e0953c6b4d1b |
|
BLAKE2-256 | dea890421662d054bfb168e3644e5f79f7fbd3d863493ee558b0fe0a1e831ef4 |
Hashes for aiohttp-1.3.2-cp35-cp35m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9e62a5c08d29211d685aebfaebae0b0f2403c81e6e1a687834e791483bcada55 |
|
MD5 | 9ed92ecc98f58b911834fd17be151f6a |
|
BLAKE2-256 | 6abcc553a0240cbc5a0837d880ecb48f6f2d13a8a67bc49a695085b1aba8fe33 |
Hashes for aiohttp-1.3.2-cp35-cp35m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | baef9521f8012d602644b4b5722b9d1e1d96d093ce0d2e065a89e3342b7f80fc |
|
MD5 | 7023382cf1f480efe70353c1df32972f |
|
BLAKE2-256 | 9ff53ea958a6a9ec54aaf274696267273975b5480a8483a51b4b96202ef6b106 |
Hashes for aiohttp-1.3.2-cp34-cp34m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 715635ca77ec8a2218b21561a907146b579d30eef732502ad30f70b8cb1bb88b |
|
MD5 | fa49c7e461fe1663c1edf1fd30cd9d5e |
|
BLAKE2-256 | aa6ee5e5550a1051850d667394125033a53bebdd8b8680b16f0d0e4394b86fa7 |
Hashes for aiohttp-1.3.2-cp34-cp34m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6f4157ec17443559f5990d68cd3247b80b4c2ab8e1b0d8947f00de80d28db0b9 |
|
MD5 | d40b2bba0b545174a2e0e351eb591578 |
|
BLAKE2-256 | 3129230ee5dd4019ea9857a335e0d0de10af8a5594ab3d163c4e58809379155b |
Hashes for aiohttp-1.3.2-cp34-cp34m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 974573cde91377c0e9e52ddf001ac0c095cb6ceee0efaa34ea173ee7a3c35d52 |
|
MD5 | 67dfb798d655deaa4209d7513820be55 |
|
BLAKE2-256 | e55e5542ec2d6c9afc1848eda5923ad27c55e86097245b19a2343a918391e075 |
Hashes for aiohttp-1.3.2-cp34-cp34m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 85e91a663201b2b36107206c975cbad3d635e52e24395ee9c4f0d220038cbe1c |
|
MD5 | 9af3332739b4874379c931d6e349d88e |
|
BLAKE2-256 | d2397a8ba6fd57bb114f8724b5f4c3400f5fec1598540bc860d0e409422d905f |