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.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.1-cp35-cp35m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7012598700bd7769c55de5a9b0949bfa2adbbed47c3c821f4cef62f2c0c824d6 |
|
MD5 | f9d8b545e74ba3fc3a272615fba7aed5 |
|
BLAKE2b-256 | d88fc33dce63672a55daebad36aac897742b29c095fa57b48a69ef6e708dd98d |
Hashes for aiohttp-1.1.1-cp35-cp35m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7f7837e00ba01205ef289eee8f45069e79c78d84890000f25b01fec01d282090 |
|
MD5 | 76c1cd6dc24abb0896c49e575382ed0a |
|
BLAKE2b-256 | d837f396473d7cd1a2fe4f8478a0b40fe999a44dcac12bb54f853f3de1ba9ea4 |
Hashes for aiohttp-1.1.1-cp35-cp35m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 60510e00023729f55d8c9b9229d43b325b763fddaf9767d71b40e2247a56f316 |
|
MD5 | 93ffd016edbf46f8fb3ab92f6aa29885 |
|
BLAKE2b-256 | 120a0356b35f12aefb262de5b5f2aa1785356d6d250e912abb90bdcd2ce0784d |
Hashes for aiohttp-1.1.1-cp35-cp35m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3698765d05b1af8ed90281de54a7d4872a6d021744c2f09e2bcd89d8a6584db9 |
|
MD5 | b38369a298cced0ebf4aaef1255f06a0 |
|
BLAKE2b-256 | fe6e6b2569b1ef078770cd78f6796e69956294f16cce8584132d0294b4170da6 |
Hashes for aiohttp-1.1.1-cp34-cp34m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 05c8cc3e7138c5e6b725d0376f2233ec9f9b3e78952d4395afa6f9b8384d663e |
|
MD5 | 8e0082303c6ab434be87de1aa41e38c0 |
|
BLAKE2b-256 | 0e3b2518410c4734f69727fb2f9ba82b06465fdf8b1f24b313d9c455e51981c1 |
Hashes for aiohttp-1.1.1-cp34-cp34m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7b8d8e93fcec1b4303c7325dfe05698221e391220001d0e84e941df4dddba030 |
|
MD5 | 6bbd1a248ef0298c9d77efe66ebf9b90 |
|
BLAKE2b-256 | 435c8a1d5b68b9fcdc3f725c2d7e10571ebe3ae3730b9d2f4a207f69ac9ec2d0 |
Hashes for aiohttp-1.1.1-cp34-cp34m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6b6f5ce6614601ee811e06ffe18a0bbc110ae6bad89f0424ce6485c2821ba5d1 |
|
MD5 | 27ff92bce2aea9affa8d0359da8fec0a |
|
BLAKE2b-256 | 8be14143a4c783815361c9b1f74f776ee63a8bfe8dea308c788775b22cce061c |
Hashes for aiohttp-1.1.1-cp34-cp34m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ecd8cf7b6ab0d99eca39ea13ee4a4faaaf095e170660cb034fc0dfe352111900 |
|
MD5 | 1ddc468c1bc9d1753c8f6f025460b1b6 |
|
BLAKE2b-256 | 6558696b7f465b4505464b216a6a5ff759b793647a5b7685522d8449eb062490 |