How to Rotate Proxies in Python with Requests (2026 Guide)

⏱️ 5 min readPython Tutorial

Websites may apply rate limits or access controls to automated requests. For permitted collection, residential proxy rotation is one configuration option alongside careful pacing and error handling.

In this tutorial, we will show you how to use the requests library in Python to rotate residential IPs automatically using the UnlimitedResidentialProxy gateway.

Why Rotate Proxies?

Static datacenter IPs can be easier for some targets to classify. Rotating residential sessions distributes authorized requests across available exits, but it does not make automation undetectable or guarantee access.

Step 1: Setup your Proxy String

Rotation is handled on the gateway level. You don't need complex Python logic to manage lists of IPs.

main.py
import requests

# ---------------------------------------------------------
# CONFIGURATION
# ---------------------------------------------------------
# Login to your dashboard to get your assigned Gateway Host
PROXY_HOST = "<YOUR_GATEWAY_HOST>" 
PROXY_PORT = "7777"
PROXY_USER = "USERNAME"
PROXY_PASS = "PASSWORD"

# Construct the Proxy String
# Note: We support HTTP, HTTPS, and SOCKS5 on the same port
proxy_url = f"http://{PROXY_USER}:{PROXY_PASS}@{PROXY_HOST}:{PROXY_PORT}"

proxies = {
    "http": proxy_url,
    "https": proxy_url,
}

target_url = "https://httpbin.org/ip"

# Make 5 requests to show rotation
for i in range(5):
    try:
        # A timeout prevents the request from waiting indefinitely
        response = requests.get(target_url, proxies=proxies, timeout=10)

        # Raise an exception for unsuccessful HTTP responses
        response.raise_for_status()

        # Display the exit IP observed for this request
        exit_ip = response.json().get("origin", "unknown")
        print(f"Request {i+1}: IP is {exit_ip}")

    except requests.exceptions.RequestException as e:
        print(f"Error: {e}")

To use SOCKS5 instead of the HTTP proxy configuration shown above, install the optional dependency with python -m pip install 'requests[socks]', then use a socks5h:// proxy URL when you want DNS resolution to occur through the proxy. The request and error-handling structure remains the same.

Understanding the Gateway

The unlimited plans provide access to a shared rotating residential pool advertised at 4M+ IPs. Rotation and sticky-session behavior depend on the selected configuration and live exit availability; a new or unique IP is not guaranteed for every request.

  • Zero Maintenance: No need to validate or refresh proxy lists manually.
  • Residential exits: Available IPs and observed results vary by location and target.
  • Geo-Targeting: Add -country-us to your username to target specific regions.

Need Unlimited Bandwidth?

Unlimited plans are priced by aggregate throughput and duration, starting at $211 for 100 Mbps for one day. Pay per GB is a separate 30-day product.