How to Rotate Proxies in Python
Anastasia Zatonskaya
Anastasia Zatonskaya Published: 2024/05/31

Today, web scraping can’t be efficient without proxies. To maximize the efficiency, advanced users create customizable Python-based proxy rotation solutions.

What is proxy rotation?

Proxy rotation is an approach when a proxy server automatically switches a user’s IP at fixed intervals of time, after a set number of requests or every time a user gets online. This approach provides even higher anonymity and security as compared to those offered by non-rotating proxies, since it creates an illusion that a request to the servers comes from another location or device.

Due to this advantage, proxy rotation has become an important security tool for companies, professionals, and data enthusiasts engaged in massive data crawling aimed at collecting data for BI, analytics, market research, and similar goals.  

Why you should use IP rotation

Actually, you can’t do without proxy rotation if you are engaged in middle-to-large-scale web scraping. This approach provides multiple advantages for those who conduct market research, ad verification and placement, multiple account management for SMM or SEO:

  1. Rotating proxies provide security and anonymity even higher than those a user can get from non-rotating proxies. Through continuously changing IPs that disguise your original IP, you get the ultimate protection against any kinds of spying and surveillance.
  2. Proxy rotation is a great tool designed to bypass all kinds of blocks and CAPTCHAS, as it’s hard to detect and ban a user that logs on under a new IP every time.
  3. If you’ve chosen a service provider with the vast IP pool, you get access to enormous scalability opportunities since you can distribute your requests over millions of IP addresses.
  4. Support in different programming languages gives users wider choice and better understanding of how to rotate proxies. With this variety and ease-of-use, users can select from multiple technical solutions to fit their specific needs.
  5. Geographically distributed and diverse pools of proxies help users rotate IPs both from all over the world or from their target region.
  6. Users that practice IP rotation get advanced data access: not only can they bypass traditional geo-restrictions, but speed up data collection through greater number of requests during the same time.

Get 20% off the original price with our special coupon!


Start Free Trial

How to rotate IP address?

In terms of the tools employed, there are three approaches to carry out proxy rotation:

Manually ProgrammaticallyWith IP rotator
Rotating IPs manually each time it is necessary to alter themEmploying a variety of software tools, including the Python Requests libraryUsing a random rotation method, usually through the dedicated proxy rotation service.

As for rotation methods, the following approaches are singled out:

  • Pre-configured (or time-based) rotation enables IP rotation with a certain frequency, such as every hour or once in five minutes.
  • Specific IP rotation permits to manually select certain IPs in order to gain stricter control over the results you want to get.
  • With random IP rotation, you can switch between pool IPs in an arbitrary manner, which improves your chances to avoid detection and ban.
  • During the burst IP rotation, a new IP is introduced after the assigned number of connections is reached.
  • Sticky proxy rotation features longer duration of the sessions, which is especially handy for the tasks relying on session persistence.
  • Request-based rotation switches IPs with every new request. This technique also prevents blocks and bans imposed by anti-scraping measures.
  • Intelligent IP rotation offered by advanced service providers automatically manages rotation sessions, choosing the best options depending on the target website and user behavior. 

How to Use Proxies to Rotate IP Addresses In Python

Many advanced users employ Python capabilities to get IP rotation up and running.

One of the offered approaches includes the following steps.

  1. Retrieve the list of proxies. Of course, it’s better to rely on the IP pool of a reputable provider, but for obvious reasons, generally accessible code samples use free proxies. Save the list of proxies to <rotating_proxies_list.txt>.
  2. To read that proxy list file and create an array with them, use the following code:
  1. To check the proxy health, use the Python requests library and the <> web page that is supposed to return an HTML response with the IP. If proxies are up and running, the output will include the status code (such as 200) and the proxy’s IP. If the proxy isn’t available or can’t handle the request, en error is outputted. 

Save this content in <> and run it from the command line:

You should get the following result:

  1. Additional checks help determine, if the response contains an IP address:
  1. Check all proxies in your array. You can call them in a sequential order; alternatively, you can run <aiohttp> and <asyncio.gather> to launch all requests simultaneously. Though making the source code more sophisticated, <async> speeds up the entire process.
  1. Distribute proxies among several sets according to their health. It makes sense to create three <sets> of proxies: healthy (working), failed (not working) and those to be checked (unchecked at the moment). The first set of <unchecked> proxies contains the proxies from the file. You can initialize a set from an array, which makes it easy to create it.
  1. You will need auxiliary functions to move proxies between sets.
  1. Don’t forget to edit <get> to call these functions after each request: 
  1. Now it’s time to check the script. Ideally, the <unchecked> set should be empty:
  1. Create the function that selects a random proxy to use it for web scraping. Since <random> doesn’t work with sets, it’s necessary to convert it using a <tuple>.
  1. Edit the <get> function to use a random proxy. If it fails, it will be added to the <not_working> set:
  1. Under the script, it’s necessary to add the content you want to scrape. The final script on how to rotate proxies in Python should look like this:
  1. Use <Timers> to recheck failed proxies. With the <reset_proxy> function, run a <Timer> for every failed proxy. The code written for this purpose should look as follows:

Another Python-based solution can be found on GitHub: GeOrg3 offers the Python library employing a large IP pool of AWS API Gateway and uses it as a proxy generating pseudo-infinite IPs. This enables web scraping of websites and services bypassing rate limits. According to the library developer, when using ApiGateway as a proxy, there is a high chance the IP will be changed every time, as the pool of AWS IPs is virtually endless.

The simple version of the code looks as follows: 

The author also offers an alternative version of the source code featuring auto-start and shutdown and warns that it’s necessary to shut down the gateways via the shutdown() method not to be charged extra cash for the exceeded number of requests.

Of course, these are only two out of the multiple options that you can find across the World Wide Web, No matter what tasks you plan to resolve, you will always find a solution tailored to your specific needs.

Tips To Build A Proxy Rotator

There are some tips that will improve the quality of your customized Python-based Proxy Rotator:

  1. Avoid predictable proxy IP addresses as they increase probability of being detected. Anti-scraping tools can easier identify requests sent from the predictable-format IPs, like those with a continuous series of number or from the same group.
  2. Enhance IP rotation with the user agent rotation: this will make the detection task more difficult for anti-scraping tools.
  3. Opt for a reliable proxy provider to enjoy high speed, performance, and guaranteed privacy you will never get with free solutions.
  4. Acquire elite proxies to maximize your anonymity and thus minimize chances of being detected.
Rate this article, if you like it:

Frequently Asked Questions

Please read our Documentation if you have questions that are not listed below.

  • Does proxy rotation have any drawbacks?

    Proxies often feature lower Internet connection speeds. Since rotation requires multiple IPs, it often results in higher costs.

  • Are there ready-to-use proxy rotation solutions?

    Reputable proxy providers often have dashboards that support IP rotation.

  • What type of proxy should you choose for proxy rotation?

    In terms of price / quality ratio, datacenter proxies are best for IP rotation, since they are cheaper than residential ones, but still rather fast and reliable for the majority of web scraping tasks.

Get 100% Clean DC & Residential Proxies

Contact Us