---
title: "How to Set Proxy in AIOHTTP"
slug: proxy-in-aiohttp
date: 2024-02-07T10:38:02+00:00
modified: 2026-08-25T05:18:57+00:00
permalink: https://brightdata.com/blog/how-tos/proxy-in-aiohttp
type: blog
---

[ Blog ](https://brightdata.com/blog "Blog") / [How Tos](https://brightdata.com/blog/how-tos)







 [How Tos](https://brightdata.com/blog/how-tos)

# How to Set Proxy in AIOHTTP

Learn how to set proxy in AIOHTTP in this step by step guide

 10 min read





 [ ](https://brightdata.com/blog/authors/antonello-zanini)

 [Antonello Zanini

Technical Writer

 ](https://brightdata.com/blog/authors/antonello-zanini)





 ![How to Set Proxy in AIOHTTP](https://media.brightdata.com/2024/02/How-to-Set-Proxy-in-AIOHTTP.svg)





At the end of this AIOHTTP proxy integration guide, you will know:

- What AIOHTTP is.
- Why you should use a proxy.
- How to set an HTTP, HTTPS, or SOCKS proxy in AIOHTTP.
- ow to deal with proxy authentication, SSL certificate errors, IP rotation, and more.

## What Is AIOHTTP?

[AIOHTTP](https://docs.aiohttp.org/en/stable/index.html) is a popular, open-source, Python async HTTP client/server framework. Its main features are:

- Support for both client and server side of HTTP protocol.
- Support for both client and server web sockets.
- Provide a web server with middleware and pluggable routing.

What matters in this tutorial is its ability to act as an HTTP client to perform HTTP requests on top of asyncio. AIOHTTP is particularly well-suited for building scripts that need to handle multiple concurrent requests efficiently.

As of this writing, AIOHTTP has over [14k stars on GitHub](https://github.com/aio-libs/aiohttp).

## Why Do You Need an AIOHTTP Proxy?

A good reason for using a proxy in AIOHTTP is to protect your online identity. By routing your request to a [proxy server](/blog/proxy-101/what-is-proxy-server), you can hide your IP and location, making the destination site believe that your request comes from the proxy server and not from you. This means that it will be more difficult to identify and track you, adding confidentiality to your online operations while avoiding IP bans and geo-restrictions.

Note that you can achieve the same result with `requests`, as covered in our [`requests` proxy integration tutorial](/blog/proxy-101/proxy-with-python-requests).

## Setting a Proxy in AIOHTTP Proxy

In this step-by-step section, you will learn how to set an HTTP, HTTPS, or SOCKS proxy in AIOHTTP.

### AIOHTTP Prerequisites

Here, we will assume that you already have Python installed on your machine and a Python project with a virtual environment in place. Install AIOHTTP and its recommended dependencies with the following command:

  ```
pip install aiohttp[speedups]
```

This will add `<a href="https://pypi.org/project/aiohttp/" rel="nofollow noopener noreferrer" target="_blank">aiohttp</a>` to your project’s dependencies.

Open your Python script file and add the `aiohttp` import:

  ```
import aiohttp
```

To make aiohttp work with the `async/await` syntax, you will have to use `asyncio`. So import that library as well and set up a basic `async main()` function:

  ```
nimport aiohttpnimport aiohttpnnimport asyncionnasync def main():n  # aiohttp logic...nnasyncio.run(main())
```

Perfect! You are ready to write the AIOHTTP proxy integration logic!

### Proxy Prerequisites

Before seeing how to integrate a proxy in AIOHTTP, you need to understand what a proxy URL looks like.

This is the format of the URL required to connect to a proxy server in AIOHTTP:

  ```
u003cPROTOCOLu003e://[u003cUSERNAMEu003e:u003cPASSWORDu003e@]u003cHOSTu003e[:u003cPORTu003e]
```

In detail, that consists of:

- `<PROTOCOL>`: `http` for HTTP proxies, `https` for HTTPS proxies, and `socks`, `socks4`, or [`socks5` for proxies](/solutions/socks5-proxies) in the different SOCKS protocols.
- `<HOST>`: The IP of the proxy server or its entire domain (e.g. `proxydomain.com`).
- `<PORT>`: The port the proxy server listens to.
- `<USERNAME>`: The optional username to authenticate the proxy connection.
- `<PASSWORD>`: The optional password to authenticate the proxy connection.

Note that`<PROTOCOL>` and `<HOST>` are the only required parts of the URL. The `<PORT>` section is optional, but you typically have to specify it. Lastly, the credentials section `<USERNAME>:<PASSWORD>` is only necessary when dealing with authenticated proxies.

If you do not have the URL of a proxy server, you can get one online for free. At the same time, keep in mind that **free proxies are unreliable, data-greedy, inconsistent, error-prone, and shady. You can use them only for learning purposes**. In production, you will need a reliable premium proxy from a top-notch provider like Bright Data.

Later in this article, you will see how to get a Bright Data proxy and use it in AIOHTTP.

### HTTP Proxy Integration

As mentioned in the documentation, AIOHTTP supports HTTP proxy integration. Assume this is the URL of your HTTP proxy:

  ```
http://190.6.23.219:999
```

You can specify it in your AIOHTTP request by using the proxy option as below:

  ```
nasync with aiohttp.ClientSession() as session:n    async with session.get(n        'https://example.com',n        proxy='http://190.6.23.219:999'n    ) as response:n        print(response.status)
```

AIOHTTP will now route the GET request through the proxy URL passed as a `proxy` argument to the `get()` function.

Keep in mind that you can use an [HTTP proxy](/solutions/https-proxies) for both HTTP and HTTPS traffic.

### HTTPS Proxy Integration

When it comes to HTTPS proxies, things become a bit more complex. As stated in the AIOHTTP documentation:

> “In Python 3.10, support for TLS in TLS is disabled for the transports that `<a href="https://docs.python.org/3/library/asyncio.html#module-asyncio" rel="nofollow noopener noreferrer" target="_blank">asyncio uses</a>`. If the further release of Python (say v3.11) toggles one attribute, it’ll just work™.
>
> aiohttp v3.8 and higher is ready for this to happen and has code in place supports TLS-in-TLS, hence sending HTTPS requests over HTTPS proxy tunnels.”

In other words, if you want to use HTTPS proxies in AIOHTTP, you need to upgrade Python to version 3.11 and `aiohttp` to v3.8 or higher. The approach to HTTPS AIOHTTP proxy integration remains the same as seen above. The only aspect that changes is that the proxy protocol will be `https` and no longer `http`.

### SOCKS Proxy Integration

AIOHTTP does not support SOCKS proxies out of the box. If you have to use a SOCKS proxy in AIOHTTP, you have to install `aiohtpp-socks`:

  ```
pip install aiohttp_socks
```

This library uses `python-socks` to extend AIOHTTP with SOCKS4(a) and SOCKS5(h) support.

Consider this as the URL of your SOCKS proxy:

  ```
socks5://98.175.31.195:4145
```

Use it in AIOHTTP as follows:

  ```
n# initialize a SOCKS proxy connectornconnector = ProxyConnector.from_url('socks5://98.175.31.195:4145')nn# initialize an AIOHTTP client with the SOCKS proxy connectornasync with aiohttp.ClientSession(connector=connector) as session:n    async with session.get('https://example.com') as response:n        print(response.status)
```

AIOHTTP will now route the GET request through the specified SOCKS proxy.

`ProxyConnector` comes from `aiohttp-socks`, so do not forget to import with:

  ```
from aiohttp_socks import ProxyConnector
```

Behind the scenes, `aiohttp-socks` defines an [AIOHTTP Connecto](https://docs.aiohttp.org/en/stable/client_advanced.html#connectors)r to tweak the requests and make them go through the given SOCKS proxy.

### Complete Example

You just learned how to use a proxy with AIOHTTP. Let’s verify that the above approaches work in a complete example!

For the sake of simplicity, we will work with an HTTP proxy, but you can easily adapt the example below to HTTPS and SOCKS.

To target URL of the GET request will be the `/ip` endpoint from the HTTPBin project. This API returns the IP of the incoming request. Thus, if the proxy integration is successful, the endpoint should respond with the IP of the proxy server and not yours.

Again, assume this is the URL of your HTTP proxy server:

  ```
http://190.6.23.219:999
```

Integrate it into AIOHTTP and print the data returned by the https://httpbin.io/ip endpoint with:

  ```
nimport aiohttpnimport asyncionnasync def main():n    async with aiohttp.ClientSession() as session:n        # perform a GET request through an HTTP proxyn        async with session.get(n          'https://httpbin.io/ip',n          proxy='http://190.6.23.219:999'n        ) as response:n            # extract the response data and print itn            json = await response.json()n            print(json)nnasyncio.run(main())
```

Execute this script, and you should get:

  ```
n{n  'origin': '190.6.23.219'n}
```

Et voilà! That is exactly the IP of the proxy server, which means that the AIOHTTP proxy integration works like a charm.

**Note**: Proxy servers are short-lived. By the time you read this tutorial, the selected proxy will no longer work and the above script will fail. To make it work, replace the HTTP proxy with a fresh and working one.

## AIOHTPP Proxy: Advanced Use Cases

You know the basics of AIOTTP proxy integration, but what about more complex techniques? Find out more in this section!

### Global Proxies Via Environment Variables

AIOHTTP supports two ways to determine which proxy to use:

1. It uses the proxy URL passed to the proxy argument.
2. If `trust_env` is set to `True`, it reads the `HTTP_PROXY` and `<a href="/solutions/https-proxies">HTTPS_PROXY</a>` environment variables.

You can therefore configure a proxy globally in AIOHTTP by setting the envs below:

- `HTTP_PROXY`: The URL of the proxy server to use for HTTP requests.
- `HTTPS_PROXY`: The URL of the proxy server to use for HTTPS requests.

For example, set them on Linux or macOS with the following commands:

  ```
nexport HTTP_PROXY=u0022u003cPROTOCOLu003e://[u003cUSERNAMEu003e:u003cPASSWORDu003e@]u003cHOSTu003e[:u003cPORTu003e]u0022nexport HTTPS_PROXY=u0022u003cPROTOCOLu003e://[u003cUSERNAMEu003e:u003cPASSWORDu003e@]u003cHOSTu003e[:u003cPORTu003e]u0022
```

Next, pass `trust_env=True` to `aiohttp.ClientSession()`:

  ```
async with aiohttp.ClientSession(trust_env=True) as session:
```

You can now call `session.get()` with no `proxy` argument

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

Great! AIOHTTP will use the proxies specified in the environment variables for each HTTP and HTTPS request.

Follow the link to learn more, as the same mechanism also works with [proxies in cURL](/blog/proxy-101/curl-with-proxies).

### Proxy Authentication Approaches

Trying to connect to an authenticated proxy without the right credentials will result in a `407 Proxy Authentication Required` error.

Suppose the string below is the URL to connect to an authenticated HTTP proxy:

  ```
http://admin:<a class="__cf_email__" data-cfemail="becef58bd7ce8786f0e9ce8b88d287cdd4c786fe8c8e908f8786908f8f8c908d8f8c" href="/cdn-cgi/l/email-protection">[email protected]</a>:8928
```

Here, the bare proxy URL is:

  ```
http://20.198.112.312:892
```

And the authentication credentials are:

- Username: `admin`
- Password: `pK5ip98NWp56l9sjy`

AIOHTTP supports two ways to deal with authenticated proxies:

1. Specify the credentials directly in the proxy URL:

  ```
nasync with session.get(n  'https://example.com/',n  proxy='http://admin:<a class="__cf_email__" data-cfemail="0878433d61783130465f783d3e64317b627130483a38263931302639393a263b393a" href="/cdn-cgi/l/email-protection">[email protected]</a>:8928'n) as response:
```

 2. Pass the username and password credentials to an `aiohttp.BasicAuth()` object and then use it in the `proxy_auth` option:

  ```
nproxy_auth = aiohttp.BasicAuth('admin', 'pK5ip98NWp56l9sjy')nasync with session.get(n    'https://example.com/',n    proxy=u0022http://20.198.112.312:892u0022,n    proxy_auth=proxy_authn) as response:
```

Awesome! AIOHTTP proxy authentication is no longer a secret.

### Avoid SSL Certificate Errors

When setting a proxy in AIOHTTP, your requests may fail because of the following error:

  ```
ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain
```

To avoid these SSL certificate issues, [turn off SSL verification with `ssl=False`](https://docs.aiohttp.org/en/stable/client_advanced.html#ssl-control-for-tcp-sockets):

  ```
nasync with aiohttp.ClientSession() as session:n    async with session.get(n      'https://example.com',n      proxy='http://190.6.23.219:999',n      ssl=False # turn of SSL certificate verificationn    ) as response:n        print(response.status)
```

The AIOHTTP SSL certificate verification errors will immediately disappear.

### Rotating Proxies in AIOHTTP

If you rely on the same proxy server over and over again, there is a chance that the target site will block its IP address. To avoid that, you must ensure that your request uses different proxy servers.

Here is how you can do it:

1. Populate a Python list with many proxy URLs.
2. Randomly pick a proxy URL before each request.
3. Set the chosen proxy URL in AIOHTTP.

The main problem with this approach is that it involves boilerplate code. Also, it requires access to a pool of reliable proxy servers, which is not free. Luckily, Bright Data has a more effective solution! Its rotating proxies automatically change the exit IP addresses for you, are available in 195 countries, boast exceptional network uptime, and guarantee a success rate of 99.9%.

In the next chapter, you will learn how to get started with Bright Data’s [rotating proxies](/solutions/rotating-proxies) in AIOHTTP.

## Integrating AIOHTTP with a Bright Data Proxy

Bright Data controls the best proxy servers in the world, serving Fortune 500 companies and 50,000+ customers. Its worldwide proxy network involves:

- [Datacenter proxies](/proxy-types/datacenter-proxies) – Over 1,300,000+ datacenter IPs.
- [Residential proxies](/proxy-types/residential-proxies) – Over 400M+ residential IPs in more than 195 countries.
- [ISP proxies](/proxy-types/isp-proxies) – Over 1,300,000+ ISP IPs.

This is one of the largest and most reliable proxy networks in the world!

Follow the steps below and learn how to use Bright Data’s proxies in AIOHTTP.

If you already have an account, log in to Bright Data. Otherwise, create an account for free. You will gain access to the following user dashboard:

Click the “View proxy products” button as below:

You will be redirected to the following “Proxies &amp; Scraping Infrastructure” page:

Scroll down, find the “Residential Proxies” card, and click on the “Get started” button:

You will reach the residential proxy configuration dashboard. Follow the guided wizard and set up the proxy service based on your needs. If you have any doubts about how to configure the proxy, feel free to contact the 24/7 support.

Go to the “Access parameters” tab and retrieve your proxy’s host, port, username, and password as follows:

Note that the “Host” field already includes the port.

That is all you need to build the proxy URL and use it in AIOHTTP. Put all the information together, and build a URL with the following syntax:

  ```
u003cUsernameu003e:u003cPasswordu003e@u003cHostu003e
```

For example, in this case it would be:

  ```
brd-customer-hl_4hgu8dwd-zone-residential:<a class="__cf_email__" data-cfemail="4913131313131313131313092b3b2d673a3c392c3b393b263130672026" href="/cdn-cgi/l/email-protection">[email protected]</a>:XXXXX
```

Toggle “Active proxy,” follow the last instructions, and you are ready to go!

Your AIOHTTP proxy snippet for Bright Data integration will look like as follows:

  ```
nimport aiohttpnimport asyncionnasync def main():n    async with aiohttp.ClientSession() as session:n        # perform a GET request through an HTTP proxyn        async with session.get(n          'https://lumtest.com/myip.json',n          proxy='http://brd-customer-hl_4hgu8dwd-zone-residential:<a class="__cf_email__" data-cfemail="9ac0c0c0c0c0c0c0c0c0c0daf8e8feb4e9efeaffe8eae8f5e2e3b4f3f5" href="/cdn-cgi/l/email-protection">[email protected]</a>:XXXXX'n        ) as response:n            # extract the response data and print itn            json = await response.json()n            print(json)nnasyncio.run(main())
```

### Conclusion

In this AIOHTTP proxy integration tutorial, you saw why you should use proxies and how to do it with `aiohttp`. You now know how to set up an HTTP, HTTPS, or SOCKS proxy in AIOHTTP, a popular Python HTTP client library. As proved here, that only takes a few lines of code!

Thanks to this guide, you also understood why you should never use free proxy services. Instead, you should adopt reliable proxy servers from a reputable proxy service provider. The best provider in the market? Bright Data! The proxy integration procedure in AIOHTTP remains the same, but the benefits of premium proxies are endless.

[Start free trial](#hs-signup)



Contact usStart free trial

No credit card required











 [ ](https://www.linkedin.com/in/antonello-zanini/)

Antonello Zanini

 Technical Writer



  5.5 years experience



Antonello Zanini is a technical writer, editor, and software engineer with 5M+ views. Expert in technical content strategy, web development, and project management.



Expertise

  Web Development   Web Scraping   AI Integration



 [ View all articles ](https://brightdata.com/blog/authors/antonello-zanini)











 Table of Contents







Dedicated Scraper APIs &amp; No-Code Scrapers

Over 1000 scrapers for all popular domains. Simplify your web scraping.

[See pricing](/pricing/web-scraper "See pricing")

Just want data? Skip scraping.

Hundreds of ready-to-use datasets from all popular domains.

[See pricing](/pricing/datasets "See pricing")







 [ ](https://news.ycombinator.com/submitlink?t=How+to+Set+Proxy+in+AIOHTTP&u=https://brightdata.com/blog/how-tos/proxy-in-aiohttp) [ ](https://www.linkedin.com/shareArticle?mini=true&title=How+to+Set+Proxy+in+AIOHTTP&url=https://brightdata.com/blog/how-tos/proxy-in-aiohttp) [ ](http://www.reddit.com/submit?title=How+to+Set+Proxy+in+AIOHTTP&url=https://brightdata.com/blog/how-tos/proxy-in-aiohttp)







##  You might also be interested in

 [ ](https://brightdata.com/blog/how-tos/set-up-proxy-in-windows-11 "How To Set Up a Proxy in Windows 11")

 [How Tos





Antonello Zanini

Technical Writer





### How To Set Up a Proxy in Windows 11

Learn the step-by-step process of setting up a proxy in Windows 11 to enhance privacy and security, ensuring a seamless and protected online experience.



 24-Dec-2023

 8 min read

 ](https://brightdata.com/blog/how-tos/set-up-proxy-in-windows-11)

 [ ](https://brightdata.com/blog/how-tos/superagent-proxy "How To Set a Proxy in SuperAgent")

 [How Tos





Aviv Besinsky





### How To Set a Proxy in SuperAgent

Learn how to set a proxy in SuperAget in this step by step guide.



 22-Nov-2023

 8 min read

 ](https://brightdata.com/blog/how-tos/superagent-proxy)

 [ ](https://brightdata.com/blog/how-tos/python-ip-rotation "How to Use Proxies to Rotate IP Addresses in Python")

 [How Tos





Sooter Saalu





### How to Use Proxies to Rotate IP Addresses in Python

Discover how to use proxies in Python for IP rotation in web scraping, where to find reliable proxies, and tips to avoid website blocks.



 07-Sep-2023

 7 min read

 ](https://brightdata.com/blog/how-tos/python-ip-rotation)
