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, *, loop):
with aiohttp.Timeout(10, loop=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', loop=loop)
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(body=text.encode('utf-8'))
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('/{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.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
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 IMCOMPATIBLE) 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.1-cp35-cp35m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fb82d4ba0de80f9438c93ca8711d10ca8101706e5e434c39d9aed98d29556072 |
|
MD5 | 5c08770a3d63c6bd79af92cb9337c995 |
|
BLAKE2b-256 | 7c53ff009ff67ad8547739a436172aec6d0846a72f47f687ff33a660a3c9d431 |
Hashes for aiohttp-1.0.1-cp35-cp35m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fb717512e271a4450b64f2b083a5f5e7adcacef4fc85d763f4f050382d49db60 |
|
MD5 | 89a5c43335243b4f477bc1c7777fdd21 |
|
BLAKE2b-256 | 3754caa5508b81562d705861b9e5d2867ad63f699b9b607ef73cc0961a3282df |
Hashes for aiohttp-1.0.1-cp35-cp35m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bc87c1cb9fb9fbab0cb29e71b438e294ecaa7e1b647b9fae60ddc47ad9cd9cde |
|
MD5 | dfdceb6b5d2dd90bbc62c44dea0b6fa0 |
|
BLAKE2b-256 | 91edb211fb605be5bad36fb00ef5caa89ce15eec416851a93364ccf9210f21a3 |
Hashes for aiohttp-1.0.1-cp35-cp35m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f54d94faf71d20073b7d58fc16c6d8c3dcbad7ac03ccf93cbd5459c6dd305494 |
|
MD5 | c14bf016215d7de402f67f7f1d83f410 |
|
BLAKE2b-256 | ef4fe211021ac8eef7af05f532fc39e0e834082f5d9aa90b8e58548def489c3e |
Hashes for aiohttp-1.0.1-cp34-cp34m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8d2596140c251172b25f549f3dd7fe0f4fc8f49aaedf8fa748520bf3b85dd740 |
|
MD5 | dddd1b9655b586d50fcefe2ec484cbe2 |
|
BLAKE2b-256 | af17cf5dce92e9309c6a3d90c94d90574c7336895e355444905a3248aa3a6e28 |
Hashes for aiohttp-1.0.1-cp34-cp34m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 58dd84ce7e869d9476dd796511daa8b887f31496d804e7f509495a07adf75dfb |
|
MD5 | c5c3d2ea66789bb6aa0aea648c73cb26 |
|
BLAKE2b-256 | b344801b158ad3277416b51fb3c2a3e61bba9d0d86e761ae72e0b17333a58acb |
Hashes for aiohttp-1.0.1-cp34-cp34m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 70ba7d230cb1422c6f2a3e5d0efd406eb8c914199ae75d1a88547d9610cc06b4 |
|
MD5 | ad17df2b8c29a5c3fc841ee4b7635bfc |
|
BLAKE2b-256 | 3f2594a2e4d932dc0d0ab40e493d99cee2f8f84610c8399bc856c9da7c1c76bc |
Hashes for aiohttp-1.0.1-cp34-cp34m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | cdf2dd1b1874232c970a35671f47368a91e34b13d74b4e9cf442555b9086689c |
|
MD5 | 645fe57f150cd9138d3fd157c8ea24f3 |
|
BLAKE2b-256 | a8c7c4790043448c555e7ea6841aed5d82959ce115181c7180e2b8eba391ff82 |