Aiohttp Documentation Release 3.7.4
Total Page:16
File Type:pdf, Size:1020Kb
aiohttp Documentation Release 3.7.4 aiohttp contributors Feb 25, 2021 CONTENTS 1 Key Features 3 2 Library Installation 5 2.1 Installing speedups altogether......................................5 3 Getting Started 7 3.1 Client example..............................................7 3.2 Server example:.............................................7 4 What’s new in aiohttp 3? 9 5 Tutorial 11 6 Source code 13 7 Dependencies 15 8 Communication channels 17 9 Contributing 19 10 Authors and License 21 11 Policy for Backward Incompatible Changes 23 12 Table Of Contents 25 12.1 Client................................................... 25 12.2 Server................................................... 79 12.3 Utilities.................................................. 162 12.4 FAQ.................................................... 177 12.5 Miscellaneous.............................................. 184 12.6 Who uses aiohttp?............................................ 246 12.7 Contributing............................................... 250 Python Module Index 255 Index 257 i ii aiohttp Documentation, Release 3.7.4 Asynchronous HTTP Client/Server for asyncio and Python. Current version is 3.7.4. CONTENTS 1 aiohttp Documentation, Release 3.7.4 2 CONTENTS CHAPTER ONE KEY FEATURES • Supports both Client and HTTP Server. • Supports both Server WebSockets and Client WebSockets out-of-the-box without the Callback Hell. • Web-server has Middlewares, Signals and plugable routing. 3 aiohttp Documentation, Release 3.7.4 4 Chapter 1. Key Features CHAPTER TWO LIBRARY INSTALLATION $ pip install aiohttp You may want to install optional cchardet library as faster replacement for chardet: $ pip install cchardet For speeding up DNS resolving by client API you may install aiodns as well. This option is highly recommended: $ pip install aiodns 2.1 Installing speedups altogether The following will get you aiohttp along with chardet, aiodns and brotlipy in one bundle. No need to type separate commands anymore! $ pip install aiohttp[speedups] 5 aiohttp Documentation, Release 3.7.4 6 Chapter 2. Library Installation CHAPTER THREE GETTING STARTED 3.1 Client example import aiohttp import asyncio async def main(): async with aiohttp.ClientSession() as session: async with session.get('http://python.org') as response: print("Status:", response.status) print("Content-type:", response.headers['content-type']) html= await response.text() print("Body:", html[:15],"...") loop= asyncio.get_event_loop() loop.run_until_complete(main()) This prints: Status: 200 Content-type: text/html; charset=utf-8 Body: <!doctype html> ... Coming from requests ? Read why we need so many lines. 3.2 Server example: from aiohttp import web async def handle(request): name= request.match_info.get('name',"Anonymous") text="Hello,"+ name return web.Response(text=text) app= web.Application() app.add_routes([web.get('/', handle), web.get('/{name}', handle)]) (continues on next page) 7 aiohttp Documentation, Release 3.7.4 (continued from previous page) if __name__ =='__main__': web.run_app(app) For more information please visit Client and Server pages. 8 Chapter 3. Getting Started CHAPTER FOUR WHAT’S NEW IN AIOHTTP 3? Go to What’s new in aiohttp 3.0 page for aiohttp 3.0 major release changes. 9 aiohttp Documentation, Release 3.7.4 10 Chapter 4. What’s new in aiohttp 3? CHAPTER FIVE TUTORIAL Polls tutorial 11 aiohttp Documentation, Release 3.7.4 12 Chapter 5. Tutorial CHAPTER SIX 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 Azure Pipelines for Continuous Integration. 13 aiohttp Documentation, Release 3.7.4 14 Chapter 6. Source code CHAPTER SEVEN DEPENDENCIES • Python 3.6+ • async_timeout • attrs • chardet • multidict • yarl • Optional cchardet as faster replacement for chardet. Install it explicitly via: $ pip install cchardet • Optional aiodns for fast DNS resolving. The library is highly recommended. $ pip install aiodns 15 aiohttp Documentation, Release 3.7.4 16 Chapter 7. Dependencies CHAPTER EIGHT COMMUNICATION CHANNELS aio-libs discourse group: https://aio-libs.discourse.group Feel free to post your questions and ideas here. gitter chat https://gitter.im/aio-libs/Lobby We support Stack Overflow. Please add aiohttp tag to your question there. 17 aiohttp Documentation, Release 3.7.4 18 Chapter 8. Communication channels CHAPTER NINE CONTRIBUTING Please read the instructions for contributors before making a Pull Request. 19 aiohttp Documentation, Release 3.7.4 20 Chapter 9. Contributing CHAPTER TEN 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. 21 aiohttp Documentation, Release 3.7.4 22 Chapter 10. Authors and License CHAPTER ELEVEN POLICY FOR BACKWARD INCOMPATIBLE CHANGES aiohttp keeps backward compatibility. After deprecating some Public API (method, class, function argument, etc.) the library guaranties the usage of depre- cated API is still allowed at least for a year and half after publishing new release with deprecation. All deprecations are reflected in documentation and raises DeprecationWarning. Sometimes we are forced to break the own rule for sake of very strong reason. Most likely the reason is a critical bug which cannot be solved without major API change, but we are working hard for keeping these changes as rare as possible. 23 aiohttp Documentation, Release 3.7.4 24 Chapter 11. Policy for Backward Incompatible Changes CHAPTER TWELVE TABLE OF CONTENTS 12.1 Client The page contains all information about aiohttp Client API: 12.1.1 Client Quickstart Eager to get started? This page gives a good introduction in how to get started with aiohttp client API. First, make sure that aiohttp is installed and up-to-date Let’s get started with some simple examples. Make a Request Begin by importing the aiohttp module, and asyncio: import aiohttp import asyncio Now, let’s try to get a web-page. For example let’s query http://httpbin.org/get: async def main(): async with aiohttp.ClientSession() as session: async with session.get('http://httpbin.org/get') as resp: print(resp.status) print(await resp.text()) loop= asyncio.get_event_loop() loop.run_until_complete(main()) Now, we have a ClientSession called session and a ClientResponse object called resp. We can get all the information we need from the response. The mandatory parameter of ClientSession.get() coroutine is an HTTP url (str or class:yarl.URL instance). In order to make an HTTP POST request use ClientSession.post() coroutine: session.post('http://httpbin.org/post', data=b'data') Other HTTP methods are available as well: 25 aiohttp Documentation, Release 3.7.4 session.put('http://httpbin.org/put', data=b'data') session.delete('http://httpbin.org/delete') session.head('http://httpbin.org/get') session.options('http://httpbin.org/get') session.patch('http://httpbin.org/patch', data=b'data') Note: Don’t create a session per request. Most likely you need a session per application which performs all requests altogether. More complex cases may require a session per site, e.g. one for Github and other one for Facebook APIs. Anyway making a session for every request is a very bad idea. A session contains a connection pool inside. Connection reusage and keep-alives (both are on by default) may speed up total performance. A session context manager usage is not mandatory but await session.close() method should be called in this case, e.g.: session= aiohttp.ClientSession() async with session.get('...'): # ... await session.close() Passing Parameters In URLs You often want to send some sort of data in the URL’s query string. If you were constructing the URL by hand, this data would be given as key/value pairs in the URL after a question mark, e.g. httpbin.org/get?key=val. Requests allows you to provide these arguments as a dict, using the params keyword argument. As an example, if you wanted to pass key1=value1 and key2=value2 to httpbin.org/get, you would use the following code: params={'key1':'value1','key2':'value2'} async with session.get('http://httpbin.org/get', params=params) as resp: expect='http://httpbin.org/get?key1=value1&key2=value2' assert str(resp.url) == expect You can see that the URL has been correctly encoded by printing the URL. For sending data with multiple values for the same key MultiDict may be used; the library support nested lists ({'key': ['value1', 'value2']}) alternative as well. It is also possible to pass a list of 2 item tuples as parameters, in that case you can specify multiple values for each key: params=[('key','value1'), ('key','value2')] async with session.get('http://httpbin.org/get', params=params) as r: expect='http://httpbin.org/get?key=value2&key=value1' assert str(r.url) == expect You can also pass str content as param, but beware – content is not encoded by library. Note that + is not encoded: 26 Chapter 12. Table Of Contents aiohttp Documentation, Release 3.7.4 async with session.get('http://httpbin.org/get', params='key=value+1') as r: assert str(r.url) =='http://httpbin.org/get?key=value+1' Note: aiohttp internally performs URL canonicalization before sending request. Canonicalization encodes host part by IDNA codec and applies requoting to path and query parts. For example URL('http://example.com//%30?a=%31') is converted to URL('http://example. com/%D0%BF%D1%83%D1%82%D1%8C/0?a=1'). Sometimes canonicalization is not desirable if server accepts exact representation and does not requote URL itself. To disable canonicalization use encoded=True parameter for URL construction: await session.get( URL('http://example.com/%30', encoded=True)) Warning: Passing params overrides encoded=True, never use both options. Response Content and Status Code We can read the content of the server’s response and its status code. Consider the GitHub time-line again: async with session.get('https://api.github.com/events') as resp: print(resp.status) print(await resp.text()) prints out something like: 200 '[{"created_at":"2015-06-12T14:06:22Z","public":true,"actor":{..