Welcome to AIOHTTP¶
Asynchronous HTTP Client/Server forasyncio and Python.
Current version is 3.11.14.
Key Features¶
Supports bothClient andHTTP Server.
Supports bothServer WebSockets andClient WebSockets out-of-the-boxwithout the Callback Hell.
Web-server hasMiddlewares,Signals and pluggable routing.
Library Installation¶
$pipinstallaiohttp
For speeding up DNS resolving by client API you may installaiodns as well.This option is highly recommended:
$pipinstallaiodns
Installing all speedups in one command¶
The following will get youaiohttp
along withaiodns andBrotli
in onebundle.No need to type separate commands anymore!
$pipinstallaiohttp[speedups]
Getting Started¶
Client example¶
importaiohttpimportasyncioasyncdefmain():asyncwithaiohttp.ClientSession()assession:asyncwithsession.get('http://python.org')asresponse:print("Status:",response.status)print("Content-type:",response.headers['content-type'])html=awaitresponse.text()print("Body:",html[:15],"...")asyncio.run(main())
This prints:
Status: 200Content-type: text/html; charset=utf-8Body: <!doctype html> ...
Coming fromrequests ? Readwhy we need so many lines.
Server example:¶
fromaiohttpimportwebasyncdefhandle(request):name=request.match_info.get('name',"Anonymous")text="Hello, "+namereturnweb.Response(text=text)app=web.Application()app.add_routes([web.get('/',handle),web.get('/{name}',handle)])if__name__=='__main__':web.run_app(app)
Development mode¶
When writing your code, we recommend enabling Python’sdevelopment mode(python-Xdev
). In addition to the extra features enabled for asyncio, aiohttpwill:
Use a strict parser in the client code (which can help detect malformed responsesfrom a server).
Enable some additional checks (resulting in warnings in certain situations).
What’s new in aiohttp 3?¶
Go toWhat’s new in aiohttp 3.0 page for aiohttp 3.0 major releasechanges.
Tutorial¶
Source code¶
The project is hosted onGitHub
Please feel free to file an issue on thebug tracker if you have found a bugor have some suggestion in order to improve the library.
Dependencies¶
attrs
multidict
yarl
Optionalaiodns for fast DNS resolving. Thelibrary is highly recommended.
$pipinstallaiodns
OptionalBrotli orbrotlicffi for brotli (RFC 7932)client compression support.
$pipinstallBrotli
Communication channels¶
aio-libs Discussions:https://github.com/aio-libs/aiohttp/discussions
Feel free to post your questions and ideas here.
Matrix:#aio-libs:matrix.org
We supportStack Overflow.Please addaiohttp tag to your question there.
Contributing¶
Please read theinstructions for contributorsbefore making a Pull Request.
Authors and License¶
Theaiohttp
package is written mostly by Nikolay Kim and Andrew Svetlov.
It’sApache 2 licensed and freely available.
Feel free to improve this package and send a pull request toGitHub.
Policy for Backward Incompatible Changes¶
aiohttp keeps backward compatibility.
After deprecating somePublic API (method, class, function argument,etc.) the library guaranties the usage ofdeprecated API is stillallowed at least for a year and half after publishing new release withdeprecation.
All deprecations are reflected in documentation and raisesDeprecationWarning
.
Sometimes we are forced to break the own rule for sake of very strongreason. Most likely the reason is a critical bug which cannot besolved without major API change, but we are working hard for keepingthese changes as rare as possible.
Table Of Contents¶
- Client
- Server
- Utilities
- FAQ
- Are there plans for an @app.route decorator like in Flask?
- Does aiohttp have a concept like Flask’s “blueprint” or Django’s “app”?
- How do I create a route that matches urls with a given prefix?
- Where do I put my database connection so handlers can access it?
- How can middleware store data for web handlers to use?
- Can a handler receive incoming events from different sources in parallel?
- How do I programmatically close a WebSocket server-side?
- How do I make a request from a specific IP address?
- What is the API stability and deprecation policy?
- How do I enable gzip compression globally for my entire application?
- How do I manage a ClientSession within a web server?
- How do I access database connections from a subapplication?
- How do I perform operations in a request handler after sending the response?
- How do I make sure my custom middleware response will behave correctly?
- Why is creating a ClientSession outside of an event loop dangerous?
- Miscellaneous
- Who uses aiohttp?
- Contributing