aiohttp¶
HTTP client/server for asyncio (PEP 3156).
Features¶
- Supports both HTTP Client and HTTP Server.
- Supports both Server WebSockets and Client WebSockets out-of-the-box.
- Web-server has Middlewares, Signals and pluggable routing.
Library Installation¶
$ pip install aiohttp
You may want to install optional cchardet library as faster replacement for chardet:
$ pip install cchardet
Getting Started¶
Client example:
import asyncio
import aiohttp
async def fetch_page(session, url):
with aiohttp.Timeout(10):
async with session.get(url) as response:
assert response.status == 200
return await response.read()
loop = asyncio.get_event_loop()
with aiohttp.ClientSession(loop=loop) as session:
content = loop.run_until_complete(
fetch_page(session, 'http://python.org'))
print(content)
Server 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'))
app = web.Application()
app.router.add_route('GET', '/{name}', handle)
web.run_app(app)
Note
Throughout this documentation, examples utilize the async/await syntax introduced by PEP 492 that is only valid for Python 3.5+.
If you are using Python 3.4, please replace await
with
yield from
and async def
with a @coroutine
decorator.
For example, this:
async def coro(...):
ret = await f()
should be replaced by:
@asyncio.coroutine
def coro(...):
ret = yield from f()
Source code¶
The project is hosted on GitHub
Please feel free to file an issue on the bug tracker if you have found a bug or have some suggestion in order to improve the library.
The library uses Travis for Continuous Integration.
Dependencies¶
Discussion list¶
aio-libs google group: https://groups.google.com/forum/#!forum/aio-libs
Feel free to post your questions and ideas here.
Contributing¶
Please read the instructions for contributors before making a Pull Request.
Authors and License¶
The aiohttp
package is written mostly by Nikolay Kim and Andrew Svetlov.
It’s Apache 2 licensed and freely available.
Feel free to improve this package and send a pull request to GitHub.
Contents¶
- HTTP Client
- Make a Request
- Passing Parameters In URLs
- Response Content
- Binary Response Content
- JSON Response Content
- Streaming Response Content
- Releasing Response
- Custom Headers
- Custom Cookies
- More complicated POST requests
- POST a Multipart-Encoded File
- Streaming uploads
- Uploading pre-compressed data
- Keep-Alive, connection pooling and cookie sharing
- Connectors
- Limiting connection pool size
- SSL control for TCP sockets
- Unix domain sockets
- Proxy support
- Response Status Codes
- Response Headers
- Response Cookies
- Response History
- WebSockets
- Timeouts
- HTTP Client Reference
- HTTP Server Usage
- HTTP Server Reference
- Abstract Classes
- Low-level HTTP Server
- Multidicts
- Working with Multipart
- Helpers API
- Logging
- Deployment using Gunicorn
- Frequently Asked Questions
- Router refactoring in 0.21
- Contributing
- CHANGES
- 0.21.1 (XX-XX-XXXX)
- 0.21.0 (02-04-2016)
- 0.20.2 (01-07-2016)
- 0.20.1 (12-30-2015)
- 0.20.0 (12-28-2015)
- 0.19.0 (11-25-2015)
- 0.18.4 (13-11-2015)
- 0.18.3 (25-10-2015)
- 0.18.2 (22-10-2015)
- 0.18.1 (20-10-2015)
- 0.18.0 (19-10-2015)
- 0.17.4 (09-29-2015)
- 0.17.3 (08-28-2015)
- 0.17.2 (08-11-2015)
- 0.17.1 (08-10-2015)
- 0.17.0 (08-04-2015)
- 0.16.6 (07-15-2015)
- 0.16.5 (06-13-2015)
- 0.16.4 (06-13-2015)
- 0.16.3 (05-30-2015)
- 0.16.2 (05-27-2015)
- 0.16.1 (05-27-2015)
- 0.16.0 (05-26-2015)
- 0.15.3 (04-22-2015)
- 0.15.2 (04-19-2015)
- 0.15.1 (03-31-2015)
- 0.15.0 (03-27-2015)
- 0.14.4 (01-29-2015)
- 0.14.3 (01-28-2015)
- 0.14.2 (01-23-2015)
- 0.14.1 (01-15-2015)
- 0.13.1 (12-31-2014)
- 0.13.0 (12-29-2014)
- 0.12.0 (12-12-2014)
- 0.11.0 (11-29-2014)
- 0.10.2 (11-19-2014)
- 0.10.1 (11-17-2014)
- 0.10.0 (11-13-2014)
- 0.9.3 (10-30-2014)
- 0.9.2 (10-16-2014)
- 0.9.1 (08-30-2014)
- 0.9.0 (07-08-2014)
- 0.8.4 (07-04-2014)
- 0.8.3 (07-03-2014)
- 0.8.2 (06-22-2014)
- 0.8.1 (06-18-2014)
- 0.8.0 (06-06-2014)
- 0.7.3 (05-20-2014)
- 0.7.2 (05-14-2014)
- 0.7.1 (04-28-2014)
- 0.7.0 (04-16-2014)
- 0.6.5 (03-29-2014)
- 0.6.4 (02-27-2014)
- 0.6.3 (02-27-2014)
- 0.6.2 (02-18-2014)
- 0.6.1 (02-17-2014)
- 0.6.0 (02-12-2014)
- 0.5.0 (01-29-2014)
- 0.4.4 (11-15-2013)
- 0.4.3 (11-15-2013)
- 0.4.2 (11-14-2013)
- 0.4.1 (11-12-2013)
- 0.4 (11-06-2013)
- 0.3 (11-04-2013)
- 0.2
- Glossary