python-aiohttpHow to reuse a session with Python Aiohttp?
Reusing a session with Python Aiohttp is easy and straightforward. You can use the session.get()
method to make a request and reuse the same session for subsequent requests.
import aiohttp
async with aiohttp.ClientSession() as session:
async with session.get('http://example.com') as response:
print(response.status)
async with session.get('http://example.com/other') as response:
print(response.status)
Output example
200
200
The code above creates a session with aiohttp.ClientSession()
and then uses the session.get()
method to make two requests to the same URL. The session is reused for both requests, so the same cookies and other session data are sent with both requests.
Code explanation
aiohttp.ClientSession()
: creates a session object.session.get()
: makes a request and reuses the same session for subsequent requests.
Helpful links
Related
- How to handle x-www-form-urlencoded with Python Aiohttp?
- How to get response code with Python Aiohttp?
- How to redirect with Python Aiohttp?
- How to create a websocket server using Python Aiohttp?
- How to create a JSON response using Python Aiohttp?
- How to download large files with Python Aiohttp?
- How to disable SSL verification in Python Aiohttp?
- How to create a connection pool with Python Aiohttp?
- How to close a Python Aiohttp session?
More of Python Aiohttp
- How to handle x-www-form-urlencoded with Python Aiohttp?
- How to rate limit with Python Aiohttp?
- How to redirect with Python Aiohttp?
- How to retry a request with Python Aiohttp?
- How to create a JSON response using Python Aiohttp?
- How to create a websocket server using Python Aiohttp?
- How to set query parameters with Python Aiohttp?
- How to use HTTP2 with Python Aiohttp?
- How to close a Python Aiohttp session?
See more codes...