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 check if a session is closed with Python Aiohttp?
- How to get a response with Python Aiohttp?
- How to make parallel requests with Python Aiohttp?
- How to handle x-www-form-urlencoded with Python Aiohttp?
- How to create a websocket server using Python Aiohttp?
- How to create a server with Python Aiohttp?
- How to use HTTP2 with Python Aiohttp?
- How to disable SSL verification in Python Aiohttp?
- How to create a JSON response using Python Aiohttp?
More of Python Aiohttp
- How to handle x-www-form-urlencoded with Python Aiohttp?
- How to get response code with Python Aiohttp?
- How to reuse a session with Python Aiohttp?
- How to get JSON data using Python Aiohttp?
- How to use a Python aiohttp client?
- How to set headers in an aiohttp request in Python?
- How to create a websocket server using Python Aiohttp?
- How to rate limit with Python Aiohttp?
- How to disable SSL verification in Python Aiohttp?
- How to use cache with Python Aiohttp?
See more codes...