9951 explained code solutions for 126 technologies


python-aiohttpHow to use HTTP2 with Python Aiohttp?


HTTP2 can be used with Python Aiohttp by using the http2 parameter in the ClientSession constructor.

import aiohttp

async with aiohttp.ClientSession(http2=True) as session:
    async with session.get('https://example.com') as response:
        print(response.status)

The output of the above code will be 200.

Code explanation

  1. import aiohttp: This imports the aiohttp library.
  2. async with aiohttp.ClientSession(http2=True) as session: This creates a ClientSession object with http2 enabled.
  3. async with session.get('https://example.com') as response: This makes a GET request to the specified URL.
  4. print(response.status): This prints the response status code.

Helpful links

Edit this code on GitHub