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 theaiohttp
module.async with aiohttp.ClientSession(keepalive=True) as session:
- creates aClientSession
object with keepalive enabled.async with session.get('http://example.com') as response:
- makes aGET
request 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 redirect with 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 check if a session is closed with Python Aiohttp?
- How to create a server with Python Aiohttp?
- How to reuse a session with Python Aiohttp?
- How to create a connection pool with Python Aiohttp?
More of Python Aiohttp
- How to handle x-www-form-urlencoded with Python Aiohttp?
- How to create a JSON response using Python Aiohttp?
- How to create a websocket server using Python Aiohttp?
- How to reuse a session with Python Aiohttp?
- How to rate limit with Python Aiohttp?
- How to use HTTP2 with Python Aiohttp?
- How to get response code with Python Aiohttp?
- How to redirect with Python Aiohttp?
- How to create a server with Python Aiohttp?
- How to make parallel requests with Python Aiohttp?
See more codes...