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
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.1.6 (2016-11-28)
Fix BodyPartReader.read_chunk bug about returns zero bytes before EOF #1428
1.1.5 (2016-11-16)
Fix static file serving in fallback mode #1401
1.1.4 (2016-11-14)
Make TestServer.make_url compatible with yarl.URL #1389
Generate informative exception on redirects from server which doesn’t provide redirection headers #1396
1.1.3 (2016-11-10)
Support root resources for sub-applications #1379
1.1.2 (2016-11-08)
Allow starting variables with an underscore #1379
Properly process UNIX sockets by gunicorn worker #1375
Fix ordering for FrozenList
Don’t propagate pre and post signals to sub-application #1377
1.1.1 (2016-11-04)
Fix documentation generation #1120
1.1.0 (2016-11-03)
Drop deprecated WSClientDisconnectedError (BACKWARD INCOMPATIBLE)
Use yarl.URL in client API. The change is 99% backward compatible but ClientResponse.url is an yarl.URL instance now. #1217
Close idle keep-alive connections on shutdown #1222
Modify regex in AccessLogger to accept underscore and numbers #1225
Use yarl.URL in web server API. web.Request.rel_url and web.Request.url are added. URLs and templates are percent-encoded now. #1224
Accept yarl.URL by server redirections #1278
Return yarl.URL by .make_url() testing utility #1279
Properly format IPv6 addresses by aiohttp.web.run_app #1139
Use yarl.URL by server API #1288
Introduce resource.url_for(), deprecate resource.url().
Implement StaticResource.
Inherit SystemRoute from AbstractRoute
Drop old-style routes: Route, PlainRoute, DynamicRoute, StaticRoute, ResourceAdapter.
Revert resp.url back to str, introduce resp.url_obj #1292
Raise ValueError if BasicAuth login has a “:” character #1307
Fix bug when ClientRequest send payload file with opened as open(‘filename’, ‘r+b’) #1306
Enhancement to AccessLogger (pass extra dict) #1303
Show more verbose message on import errors #1319
Added save and load functionality for CookieJar #1219
Added option on StaticRoute to follow symlinks #1299
Force encoding of application/json content type to utf-8 #1339
Fix invalid invocations of errors.LineTooLong #1335
Websockets: Stop async for iteration when connection is closed #1144
Ensure TestClient HTTP methods return a context manager #1318
Raise ClientDisconnectedError to FlowControlStreamReader read function if ClientSession object is closed by client when reading data. #1323
Document deployment without Gunicorn #1120
Add deprecation warning for MD5 and SHA1 digests when used for fingerprint of site certs in TCPConnector. #1186
Implement sub-applications #1301
Don’t inherit web.Request from dict but implement MutableMapping protocol.
Implement frozen signals
Don’t inherit web.Application from dict but implement MutableMapping protocol.
Support freezing for web applications
Accept access_log parameter in web.run_app, use None to disable logging
Don’t flap tcp_cork and tcp_nodelay in regular request handling. tcp_nodelay is still enabled by default.
Improve performance of web server by removing premature computing of Content-Type if the value was set by web.Response constructor.
While the patch boosts speed of trivial web.Response(text=’OK’, content_type=’text/plain) very well please don’t expect significant boost of your application – a couple DB requests and business logic is still the main bottleneck.
Boost performance by adding a custom time service #1350
Extend ClientResponse with content_type and charset properties like in web.Request. #1349
Disable aiodns by default #559
Don’t flap tcp_cork in client code, use TCP_NODELAY mode by default.
Implement web.Request.clone() #1361
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.1.6-cp35-cp35m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 62ca95054ee7bb5baa8569ac95ccdb5dfdd68c3604bb8038ab2b7e1d15b7194c |
|
MD5 | 1d146373473edac9406c56e3c5430def |
|
BLAKE2b-256 | 4ae8df4d729db4e431b91af0de2a56f4249a69365b4522d18448b7a1704470b8 |
Hashes for aiohttp-1.1.6-cp35-cp35m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1c8fe882f37293c6a3d80d42d4dc987dbab9e88b1c1441de7206e83b20892a9b |
|
MD5 | 2e401a5af6d5f2fb3f4938ce3414cbad |
|
BLAKE2b-256 | d274a5a6fb122e6fa9c75fd926a501fbd2a1a478ec2f19ac7d5801500e5b05b2 |
Hashes for aiohttp-1.1.6-cp35-cp35m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7ca2679e549eedff67b7fe8011a8ff854355228d99f702e0c4cdb571b231a52a |
|
MD5 | 71ce2ec9d7740601ce4ee0aecb0a3d69 |
|
BLAKE2b-256 | 285dd8fc4caf95baf8716f245de95365ac25217836f0c8c4be9caec27972f294 |
Hashes for aiohttp-1.1.6-cp35-cp35m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 286f14c6df810c346e32dc49fceeeac027917f18a02f12759dd9a59d27731974 |
|
MD5 | 338b372a46da2c732c909f554b065d2e |
|
BLAKE2b-256 | 256c8a7c39ee3a58c0c5e325475a19777142451a79174343d058df8edf57a107 |
Hashes for aiohttp-1.1.6-cp34-cp34m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 73bf30c378e8ed89279600b19d78f21046af06cd3e88bfd7dd77449307d800a2 |
|
MD5 | 5f929c5a2d85c3ff9f65b8bfbaf9ff28 |
|
BLAKE2b-256 | a7ea3299ecebfb68af6e1d383716bec01831490d5166a4919b977cd6df66572c |
Hashes for aiohttp-1.1.6-cp34-cp34m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | aac01b6438d9a76a0504d18a2d17681bcbd82d3b9479208288f287db55bbb095 |
|
MD5 | 689ef0c18527fa97fe72db08bc374dd7 |
|
BLAKE2b-256 | 600a4d0dddbcfc20a3f3ee6be18b8fa5d4de9a0f37ecc6b6473f82ce30e8479b |
Hashes for aiohttp-1.1.6-cp34-cp34m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e9414c0a500f1f8b3c8df709413177e9cf7f7e283151747c347d4cc7067add66 |
|
MD5 | 5cc4dea925757b6deb8588629610ecfb |
|
BLAKE2b-256 | 28545418cafbee5e9d2daf77ec4355d027518b26b1ef5833c19f092a1c765b89 |
Hashes for aiohttp-1.1.6-cp34-cp34m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ceb6ade3ed9cc11a279182ad41da3757008d089d2c3e87066a7dc8bc3d8a6436 |
|
MD5 | 816d7d0dc8b6fa8a70f7f0befe17bd51 |
|
BLAKE2b-256 | e4afb0d6fdf3b55592861addc806f2c5fc7edb6bb60567bb0d51d1dae3824f1e |