Last modified: Sep 13, 2026

Python IP Rotation Guide

IP rotation is a technique used to change the IP address of a device or application. It is commonly used in web scraping, automation, and bypassing rate limits on websites. This guide will show you how to rotate IP addresses in Python using different methods.

Why Rotate IP Addresses?

Websites often block requests from a single IP address after a certain number of attempts. By rotating IP addresses, you can avoid being blocked and continue accessing data. IP rotation also helps maintain anonymity when performing automated tasks.

Methods to Rotate IP in Python

There are several ways to rotate IP addresses in Python. The most common methods include using proxy servers, rotating proxies, and Tor network. Let's explore each method in detail.

Using Proxy Servers

Proxy servers act as intermediaries between your computer and the internet. They forward your requests and hide your original IP address. You can use HTTP or HTTPS proxies to rotate your IP address.

To use a proxy in Python, you can configure the requests library with proxy settings. Here's an example:


import requests

# List of proxy servers
proxies = [
    {'http': 'http://192.168.1.1:8080', 'https': 'http://192.168.1.1:8080'},
    {'http': 'http://192.168.1.2:8080', 'https': 'http://192.168.1.2:8080'},
    {'http': 'http://192.168.1.3:8080', 'https': 'http://192.168.1.3:8080'}
]

# Function to make request with rotating proxy
def make_request_with_proxy(url, proxy_list):
    for proxy in proxy_list:
        try:
            response = requests.get(url, proxies=proxy, timeout=10)
            return response
        except requests.exceptions.RequestException as e:
            print(f"Failed with proxy {proxy}: {e}")
    return None

# Example usage
response = make_request_with_proxy('https://httpbin.org/ip', proxies)
if response:
    print(response.json())

{'origin': '192.168.1.1'}

In this example, we define a list of proxy servers and iterate through them until a successful request is made. If one proxy fails, the next one is tried automatically.

Using Rotating Proxy Services

Rotating proxy services automatically change your IP address after each request or after a set time interval. These services are more reliable than manual proxy rotation.

Popular rotating proxy services include BrightData, Oxylabs, and SmartProxy. To use them, you typically need to authenticate with username and password credentials.


import requests
from itertools import cycle

# List of rotating proxy credentials
proxy_list = [
    'username:pass1@proxy1.example.com:8000',
    'username:pass2@proxy2.example.com:8000',
    'username:pass3@proxy3.example.com:8000'
]

# Create a cycle of proxies
proxy_pool = cycle(proxy_list)

def make_request_with_rotating_proxy(url):
    # Get next proxy from pool
    proxy = next(proxy_pool)
    proxies = {
        'http': f'http://{proxy}',
        'https': f'https://{proxy}'
    }
    
    try:
        response = requests.get(url, proxies=proxies, timeout=10)
        return response
    except requests.exceptions.RequestException as e:
        print(f"Request failed: {e}")
        return None

# Example usage
for i in range(5):
    response = make_request_with_rotating_proxy('https://httpbin.org/ip')
    if response:
        print(f"Request {i+1}: {response.json()}")

Request 1: {'origin': '104.236.191.122'}
Request 2: {'origin': '192.241.138.231'}
Request 3: {'origin': '165.227.87.104'}
Request 4: {'origin': '138.197.157.45'}
Request 5: {'origin': '192.241.138.231'}

This approach ensures that each request uses a different IP address from the pool, reducing the chance of being blocked.

Using Tor Network

The Tor (The Onion Router) network provides free anonymous browsing by routing traffic through multiple nodes. You can use Tor in Python with the selenium library and Tor browser.

To use Tor, you need to install Tor and configure it to listen on a specific port. Then you can route your requests through the Tor network.


import requests

# Configure session to use Tor
session = requests.Session()
# Tor default proxy port
session.proxies = {
    'http': 'socks5h://127.0.0.1:9050',
    'https': 'socks5h://127.0.0.1:9050'
}

# Make request through Tor
try:
    response = session.get('https://httpbin.org/ip', timeout=15)
    print(response.json())
except requests.exceptions.RequestException as e:
    print(f"Tor request failed: {e}")

{'origin': '176.31.7.129'}

You can also restart the Tor service to get a new IP address. This can be done programmatically using subprocess calls to the Tor control port.

Best Practices for IP Rotation

When implementing IP rotation, consider these best practices:

  • Use legitimate proxy services - Avoid free proxies which may be unreliable or malicious.
  • Implement error handling - Always handle exceptions when making requests with proxies.
  • Respect website terms of service - Do not use IP rotation to violate website policies.
  • Limit request frequency - Even with IP rotation, avoid overwhelming servers.
  • Monitor success rates - Track how often requests succeed with each proxy.

Considerations and Limitations

IP rotation is not a foolproof solution. Some websites use advanced detection methods beyond IP tracking. These include browser fingerprinting, cookie tracking, and behavioral analysis. For robust anonymity, combine IP rotation with other techniques like user agent rotation and headless browser configuration.

Additionally, free proxies and Tor nodes are often blacklisted by many services. Paid rotating proxy services offer better reliability and speed for serious applications.

Conclusion

Rotating IP addresses in Python is essential for web scraping and automation tasks. You can use proxy servers, rotating proxy services, or the Tor network depending on your needs and budget. Always implement proper error handling and respect website policies when making automated requests. With the right approach, IP rotation can help you gather data efficiently while avoiding blocks and maintaining anonymity.