Random Proxy Selection with urllib3

  • Share this:

Code introduction


This function uses the urllib3 library to randomly select a proxy from a predefined list and sends an HTTP GET request to example.com through the selected proxy. If the request is successful, it returns the proxy, otherwise it continues to try the next proxy.


Technology Stack : urllib3

Code Type : Function

Code Difficulty : Intermediate


                
                    
import urllib3

def get_random_proxy():
    """
    This function uses the `urllib3` library to get a random proxy from a predefined list.
    """
    proxy_list = [
        'http://10.10.1.10:3128',
        'http://10.10.1.11:3128',
        'http://10.10.1.12:3128',
        'http://10.10.1.13:3128',
        'http://10.10.1.14:3128'
    ]
    http = urllib3.PoolManager()
    for proxy in proxy_list:
        http.proxy = proxy
        try:
            response = http.request('GET', 'http://example.com')
            if response.status == 200:
                return proxy
        except urllib3.exceptions.MaxRetryError:
            continue
    return None                
              
Tags: