python-aiohttpHow to use keepalive with Python Aiohttp?
Using keepalive with Python Aiohttp is easy. To enable keepalive, you need to set the keepalive parameter to True when creating the ClientSession object.
import aiohttp
async with aiohttp.ClientSession(keepalive=True) as session:
async with session.get('http://example.com') as response:
print(response.status)
Output example
200
The code above creates a ClientSession object with keepalive enabled, then makes a GET request to http://example.com and prints the response status.
The parts of the code are:
import aiohttp- imports theaiohttpmodule.async with aiohttp.ClientSession(keepalive=True) as session:- creates aClientSessionobject with keepalive enabled.async with session.get('http://example.com') as response:- makes aGETrequest tohttp://example.com.print(response.status)- prints the response status.
Helpful links
Related
- How to handle x-www-form-urlencoded with Python Aiohttp?
- How to create a websocket server using Python Aiohttp?
- How to check if a session is closed with Python Aiohttp?
- How to create a server with Python Aiohttp?
- How to disable SSL verification in Python Aiohttp?
- How to close a Python Aiohttp session?
- How to use HTTPS with Python Aiohttp?
- How to create a connection pool with Python Aiohttp?
- How to redirect with Python Aiohttp?
- How to retry a request with Python Aiohttp?
More of Python Aiohttp
- How to handle x-www-form-urlencoded with Python Aiohttp?
- How to create a websocket server using Python Aiohttp?
- How to check if a session is closed with Python Aiohttp?
- How to get response text with Python Aiohttp?
- How to redirect with Python Aiohttp?
- How to use a Python aiohttp client?
- How to get response code with Python Aiohttp?
- How to use Python Aiohttp and FastAPI?
- How to reuse a session with Python Aiohttp?
- How to rate limit with Python Aiohttp?
See more codes...