How to Rotate Proxies in Python with ProxyHub

Rotating your proxy IPs is essential to avoid bans, distribute load, and ensure reliable scraping. In this guide, we’ll walk through how to integrate ProxyHub’s IP rotation endpoints into a Python scraping workflow in just a few lines of code.

📖 Table of Contents

Why Rotate Proxies?

Getting Started

  1. Sign up at ProxyHub and grab your API key.
  2. Install requests:
    pip install requests
  3. Define a simple helper in Python:
    import requests
    
    API_KEY = "YOUR_API_KEY"
    BASE = f"https://proxy.montgomerynx.com/{API_KEY}"

Fetch Your Current Proxy IP

Before rotating, you may want to know which IP you’re currently using:

def get_current_ip():
    res = requests.get(f"{BASE}/ip")
    res.raise_for_status()
    return res.json()["ip"]

print("Current exit IP:", get_current_ip())

Endpoint: GET /{api_key}/ip
Returns: { "ip": "123.45.67.89" }

Force a Proxy Rotation

To obtain a fresh exit IP on demand:

def rotate_ip():
    res = requests.get(f"{BASE}/force_rotate_ip")
    res.raise_for_status()
    return res.json()["ip"]

new_ip = rotate_ip()
print("Rotated to new IP:", new_ip)

Endpoint: GET /{api_key}/force_rotate_ip
Returns: { "ip": "98.76.54.32" }

Putting It All Together

import requests
from time import sleep

API_KEY = "YOUR_API_KEY"
BASE = f"https://proxy.montgomerynx.com/{API_KEY}"
TARGET_URL = "https://httpbin.org/ip"

def get_current_ip():
    return requests.get(f"{BASE}/ip").json()["ip"]

def rotate_ip():
    return requests.get(f"{BASE}/force_rotate_ip").json()["ip"]

def fetch_with_retries(url, max_retries=3):
    for attempt in range(1, max_retries + 1):
        resp = requests.get(f"{BASE}/{url}")
        if resp.status_code == 200:
            return resp.text
        print(f"Attempt {attempt} failed (status {resp.status_code}), rotating IP…")
        rotate_ip()
        sleep(1)
    resp.raise_for_status()

if __name__ == "__main__":
    print("Starting IP:", get_current_ip())
    result = fetch_with_retries(TARGET_URL)
    print("Fetched data:", result)
    print("Final IP:", get_current_ip())

Best Practices &s; Tips

With just a couple of helper functions and two API calls, you can build a robust, rotation-enabled Python scraper in under 20 lines of code. Happy scraping!