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
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.0.5 (2016-10-11)
Fix StreamReader._read_nowait to return all available data up to the requested amount #1297
1.0.4 (2016-09-22)
Fix FlowControlStreamReader.read_nowait so that it checks whether the transport is paused #1206
1.0.2 (2016-09-22)
Make CookieJar compatible with 32-bit systems #1188
Add missing WSMsgType to web_ws.__all__, see #1200
Fix CookieJar ctor when called with loop=None #1203
Fix broken upper-casing in wsgi support #1197
1.0.1 (2016-09-16)
Restore aiohttp.web.MsgType alias for aiohttp.WSMsgType for sake of backward compatibility #1178
Tune alabaster schema.
Use text/html content type for displaying index pages by static file handler.
Fix AssertionError in static file handling #1177
Fix access log formats %O and %b for static file handling
Remove debug setting of GunicornWorker, use app.debug to control its debug-mode instead
1.0.0 (2016-09-16)
Change default size for client session’s connection pool from unlimited to 20 #977
Add IE support for cookie deletion. #994
Remove deprecated WebSocketResponse.wait_closed method (BACKWARD INCOMPATIBLE)
Remove deprecated force parameter for ClientResponse.close method (BACKWARD INCOMPATIBLE)
Avoid using of mutable CIMultiDict kw param in make_mocked_request #997
Make WebSocketResponse.close a little bit faster by avoiding new task creating just for timeout measurement
Add proxy and proxy_auth params to client.get() and family, deprecate ProxyConnector #998
Add support for websocket send_json and receive_json, synchronize server and client API for websockets #984
Implement router shourtcuts for most useful HTTP methods, use app.router.add_get(), app.router.add_post() etc. instead of app.router.add_route() #986
Support SSL connections for gunicorn worker #1003
Move obsolete examples to legacy folder
Switch to multidict 2.0 and title-cased strings #1015
{FOO}e logger format is case-sensitive now
Fix logger report for unix socket 8e8469b
Rename aiohttp.websocket to aiohttp._ws_impl
Rename aiohttp.MsgType tp aiohttp.WSMsgType
Introduce aiohttp.WSMessage officially
Rename Message -> WSMessage
Remove deprecated decode param from resp.read(decode=True)
Use 5min default client timeout #1028
Relax HTTP method validation in UrlDispatcher #1037
Pin minimal supported asyncio version to 3.4.2+ (loop.is_close() should be present)
Remove aiohttp.websocket module (BACKWARD INCOMPATIBLE) Please use high-level client and server approaches
Link header for 451 status code is mandatory
Fix test_client fixture to allow multiple clients per test #1072
make_mocked_request now accepts dict as headers #1073
Add Python 3.5.2/3.6+ compatibility patch for async generator protocol change #1082
Improvement test_client can accept instance object #1083
Simplify ServerHttpProtocol implementation #1060
Add a flag for optional showing directory index for static file handling #921
Define web.Application.on_startup() signal handler #1103
Drop ChunkedParser and LinesParser #1111
Call Application.startup in GunicornWebWorker #1105
Fix client handling hostnames with 63 bytes when a port is given in the url #1044
Implement proxy support for ClientSession.ws_connect #1025
Return named tuple from WebSocketResponse.can_prepare #1016
Fix access_log_format in GunicornWebWorker #1117
Setup Content-Type to application/octet-stream by default #1124
Deprecate debug parameter from app.make_handler(), use Application(debug=True) instead #1121
Remove fragment string in request path #846
Use aiodns.DNSResolver.gethostbyname() if available #1136
Fix static file sending on uvloop when sendfile is available #1093
Make prettier urls if query is empty dict #1143
Fix redirects for HEAD requests #1147
Default value for StreamReader.read_nowait is -1 from now #1150
aiohttp.StreamReader is not inherited from asyncio.StreamReader from now (BACKWARD INCOMPATIBLE) #1150
Streams documentation added #1150
Add multipart coroutine method for web Request object #1067
Publish ClientSession.loop property #1149
Fix static file with spaces #1140
Fix piling up asyncio loop by cookie expiration callbacks #1061
Drop Timeout class for sake of async_timeout external library. aiohttp.Timeout is an alias for async_timeout.timeout
use_dns_cache parameter of aiohttp.TCPConnector is True by default (BACKWARD INCOMPATIBLE) #1152
aiohttp.TCPConnector uses asynchronous DNS resolver if available by default (BACKWARD INCOMPATIBLE) #1152
Conform to RFC3986 - do not include url fragments in client requests #1174
Drop ClientSession.cookies (BACKWARD INCOMPATIBLE) #1173
Refactor AbstractCookieJar public API (BACKWARD INCOMPATIBLE) #1173
Fix clashing cookies with have the same name but belong to different domains (BACKWARD INCOMPATIBLE) #1125
Support binary Content-Transfer-Encoding #1169
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.0.5-cp35-cp35m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3bfcb76553d7f6296d1a598162d5fb890198f98c021540cbbb85bb604ff198db |
|
MD5 | b7418ae8ba0dfdb91f3ee4e4cee84840 |
|
BLAKE2b-256 | 11e2a9d6dec7447710b3e7f69b6a0ba79b4adf71e5667654060e138ab94ae3b7 |
Hashes for aiohttp-1.0.5-cp35-cp35m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c579ec606f25b3f756f177fee6db344f8d7ef75cfc0603a94c9fa1d1c645789d |
|
MD5 | f765e3cd7dc70b398f47a5180bffc298 |
|
BLAKE2b-256 | 727eb08a7b86cc7824486a26dc8ae708e58f417c2aa3e08cfc54fe12564fe7d3 |
Hashes for aiohttp-1.0.5-cp35-cp35m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 714c62532ca6be90be4b54002743e7ea277ec78b45f04ae86cdc6f45a8400abd |
|
MD5 | 5d525802c337829fe2c17a448adbe977 |
|
BLAKE2b-256 | 635ec89bd7117b235a2f3168afea2b570dbab752d70fdaf7f4f243a80386792d |
Hashes for aiohttp-1.0.5-cp35-cp35m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e1985766a4c83fcbdf7dde06544231fc9fb3de8929788179e623d6f9f9f321d2 |
|
MD5 | f68f98484e7386d3547926bf02b4a3a6 |
|
BLAKE2b-256 | c44b83fc421303f3e8a575d66c7ae035cfc603bb9942b37b7b51bcb3624da32a |
Hashes for aiohttp-1.0.5-cp34-cp34m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3a253332a0d8f82549e65035ebe7199580c3ea0e47071b7428f25b109b3c0310 |
|
MD5 | fdef147c023a3d60f2fca8f5b7d4fe8e |
|
BLAKE2b-256 | 3c271a76748bc91024e9c379d7d7bbada64afca064e03a5e887cd83aff2aa92d |
Hashes for aiohttp-1.0.5-cp34-cp34m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3ac6fa105355928e2fc02e876d0d72d6230557d4637017ebf09aec7611124155 |
|
MD5 | 371722c75d82387e66d55016d9ba8daa |
|
BLAKE2b-256 | c81b9bf9042cce322790398547a3e62feb0c15d972ef0fb9410a213a9b879ac5 |
Hashes for aiohttp-1.0.5-cp34-cp34m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ad374a5d7be1de271ecd0fb0ef87c0f8dd9fb062e6fae350fa6c098360018978 |
|
MD5 | b625aad1f8d6bbe7765f8e675ce0cf07 |
|
BLAKE2b-256 | f6b35eebf91e86a32f30edeab6b1fba387d2173ca12bf57fc034fc046ab78bb5 |
Hashes for aiohttp-1.0.5-cp34-cp34m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 81a6aaace2b9e8a87531277a5d5f998efbd3554c15bf47173834386966d1bbe1 |
|
MD5 | 2988237ccce7a44755f33d52f5eddc63 |
|
BLAKE2b-256 | 0f152c3d937e71d285cd34dfc14f5479753926f29bf7a5816df4b14406ec1dd2 |