9951 explained code solutions for 126 technologies


python-aiohttpHow to use a proxy with a Python aiohttp client?


Using a proxy with a Python aiohttp client is easy. All you need to do is to pass the proxy URL as an argument to the ProxyConnector class.

import aiohttp

proxy_url = 'http://user:[email protected]:3128'

async with aiohttp.ClientSession(connector=aiohttp.ProxyConnector(proxy_url=proxy_url)) as session:
    async with session.get('http://example.org') as response:
        print(response.status)

Output example

200

The code above creates a ClientSession object with a ProxyConnector object as an argument. The ProxyConnector object takes the proxy URL as an argument. The ClientSession object is then used to make a GET request to http://example.org using the proxy.

Code explanation

  1. import aiohttp: imports the aiohttp library.
  2. proxy_url = 'http://user:[email protected]:3128': sets the proxy URL.
  3. aiohttp.ClientSession(connector=aiohttp.ProxyConnector(proxy_url=proxy_url)): creates a ClientSession object with a ProxyConnector object as an argument.
  4. session.get('http://example.org'): makes a GET request to http://example.org using the proxy.

Helpful links

Edit this code on GitHub