Rotating Proxies in Python: A Comprehensive Guide with ProxyTee
Complete guide to rotating IP addresses in Python using Requests, HTTPX, and AIOHTTP with ProxyTee unlimited residential proxy ports.

Complete guide to rotating IP addresses in Python using Requests, HTTPX, and AIOHTTP with ProxyTee unlimited residential proxy ports.
Web scraping is a powerful technique for gathering data, but it often comes with challenges, particularly the risk of being blocked by target websites. Rotating proxies is a vital strategy to overcome these hurdles. This guide will demonstrate how to implement proxy rotation using Python, with a focus on how ProxyTee can enhance this process.
Why Rotate Proxies?
Websites are designed to detect and block automated traffic. When numerous requests originate from the same IP address, it raises suspicion and can lead to your IP being banned. By rotating proxies, you change your IP address for each request, making your scraping activities appear as normal user behavior. This helps maintain anonymity and bypass anti-scraping measures.
While you can use third-party proxy rotation tools, building your own rotator with Python gives you more control and understanding of the process. Below, you’ll see how to do just that.
Setting Up Your Python Environment
First, create a virtual environment to keep your project dependencies isolated:
virtualenv venv
Activate the environment:
source venv/bin/activate
Then install the requests library:
pip install requests
Sending a Test Query Without Proxies
To see your current IP, create a Python file (e.g., no_proxy.py) with the following code:
import requests
response = requests.get('https://ip.oxylabs.io/location')
print(response.text)
Run the script, and your IP address will be printed.
Sending Requests Through a Proxy
To send a request using a single proxy, you need the proxy’s scheme (HTTP/HTTPS), IP address, port, and optionally a username and password. A complete proxy URL is formatted as follows:
SCHEME://USERNAME:PROXY_PASS@YOUR_PROXY_IP:YOUR_PROXY_PORT
Examples:
http://2.56.215.247:3128
https://2.56.215.247:8091
https://my-user:aegi1Ohz@2.56.215.247:8044
Modify your script to include proxy settings, add error handling with ProxyError, ReadTimeout and ConnectTimeout to handle any connection problems and monitor proxy health. You should define your proxy map like this:
scheme_proxy_map = {
'http': PROXY1,
'https': PROXY2,
'https://example.org': PROXY3,
}
Then add the following lines into the script:
import requests
from requests.exceptions import ProxyError, ReadTimeout, ConnectTimeout
try:
response = requests.get('https://ip.oxylabs.io/location', proxies=scheme_proxy_map, timeout=TIMEOUT_IN_SECONDS)
except (ProxyError, ReadTimeout, ConnectTimeout) as error:
print('Unable to connect to the proxy:', error)
else:
print(response.text)
Running this modified script will now show your proxy’s IP address.
Rotating Proxies Using a Proxy Pool
To rotate through multiple proxies, you'll need a CSV file (e.g., proxies.csv), each proxy on a new line. Update your script to read the CSV, create scheme_proxy_map for each proxy, and attempt a connection. Here is a sample:
http://2.56.215.247:3128
https://88.198.24.108:8080
http://50.206.25.108:80
http://68.188.59.198:80
Here’s how to process them in Python:
import requests
from requests.exceptions import ProxyError, ReadTimeout, ConnectTimeout
import csv
TIMEOUT_IN_SECONDS = 10
CSV_FILENAME = 'proxies.csv'
with open(CSV_FILENAME) as open_file:
reader = csv.reader(open_file)
for csv_row in reader:
scheme_proxy_map = {
'https': csv_row[0],
}
try:
response = requests.get('https://ip.oxylabs.io/location', proxies=scheme_proxy_map, timeout=TIMEOUT_IN_SECONDS)
except (ProxyError, ReadTimeout, ConnectTimeout) as error:
pass
else:
print(response.text)
break
This script iterates through each proxy in the CSV, making a request. A break statement ensures that only the first successful proxy result is printed, and the process stops after the first good result.
Asynchronous Proxy Rotation
For faster performance, especially with larger lists, use asynchronous processing. Install aiohttp:
pip install aiohttp
Here’s an asynchronous method of rotating proxy IP:
import asyncio
import aiohttp
import csv
CSV_FILENAME = 'proxies.csv'
URL_TO_CHECK = 'https://ip.oxylabs.io/location'
TIMEOUT_IN_SECONDS = 10
async def check_proxy(url, proxy):
try:
session_timeout = aiohttp.ClientTimeout(
total=None,
sock_connect=TIMEOUT_IN_SECONDS,
sock_read=TIMEOUT_IN_SECONDS
)
async with aiohttp.ClientSession(timeout=session_timeout) as session:
async with session.get(url, proxy=proxy, timeout=TIMEOUT_IN_SECONDS) as resp:
print(await resp.text())
except Exception as error:
print('Proxy responded with an error: ', error)
return
async def main():
tasks = []
with open(CSV_FILENAME) as open_file:
reader = csv.reader(open_file)
for csv_row in reader:
task = asyncio.create_task(check_proxy(URL_TO_CHECK, csv_row[0]))
tasks.append(task)
await asyncio.gather(*tasks)
asyncio.run(main())
This version runs proxy checks concurrently, providing a speed improvement.
Tips for Successful Proxy Rotation
- Avoid Free Proxies: Free proxies are slow, unreliable, and pose security risks. They often lack encrypted HTTPS connections.
- Rotate User Agents: Complement IP rotation by rotating user-agent strings to avoid detection based on browser and device information.
- Choose a Reliable Premium Service: Using a premium proxy provider is essential for a secure, reliable and fast solution.
ProxyTee: A Superior Proxy Solution
ProxyTee offers a range of rotating residential proxies that make proxy management easier. With unlimited bandwidth, global IP coverage, and auto-rotation features, you can focus on extracting data without worrying about infrastructure. ProxyTee supports multiple protocols, such as HTTP and SOCKS5, making integration into existing setups a breeze. The user-friendly GUI and simple API of ProxyTee further enhance your control over the proxies. Our Unlimited Residential Proxies offer affordable solutions, rotating IPs and ensuring specific geo-targeting to enhance your web scraping efforts.
Conclusion
Rotating proxies is critical for web scraping projects. While building a rotator in Python is manageable, services like ProxyTee streamline this process by handling many of the complexities for you. With features like unlimited bandwidth and auto-rotation, you can enhance your efficiency. Whether you are engaged in web scraping, streaming, or any other online activity needing anonymity, ProxyTee offers dependable and effective proxy services.
Scale Your Web Scraping with ProxyTee
Ready to eliminate IP bans and per-gigabyte bandwidth overages? Explore ProxyTee Unlimited Residential Proxies, learn how our unlimited bandwidth residential proxy ports work, check our proxies for travel fare aggregation, or test endpoints on our free proxy list.
Frequently Asked Questions
Target websites use ASN filtering, rate limiting, and TLS fingerprinting to block datacenter IP ranges. Routing your requests through ProxyTee's 30M+ real household residential IPs across 100+ countries ensures high success rates without CAPTCHA interruptions.
Principal infrastructure engineer at ProxyTee specializing in rotating residential proxy networks, SOCKS5/HTTP tunneling, TLS fingerprint mitigation, and distributed data extraction pipelines.


