Skip to main content
ProxyCrown

How do I rotate proxies in Python?

The short answer

With a rotating gateway, set the gateway once in the proxies dict and send requests as usual: each request gets a new IP. With a plain list of IPs, pick one per request, for example with itertools.cycle or random.choice, and drop the ones that keep failing.

Answered by the ProxyCrown team, the people who run the network this is about · Updated

The longer answer

A rotating gateway is much less code: no list to refresh, no dead IPs to track, and geo-targeting is just a username parameter.

If you rotate through a list, do not push every request through one requests.Session. A session keeps connections open to the same proxy.

import requests

proxy = "http://USER:PASS@gw.proxycrown.com:823"
for _ in range(3):
    r = requests.get("https://ipinfo.io/ip", proxies={"http": proxy, "https": proxy}, timeout=30)
    print(r.text)  # a different IP each time

Try it yourself

People also ask