ProxyForge Download

How to Test a Proxy List in Python (Fast, Async, Reliable)

Introduction

Testing proxy lists is an essential part of any web scraping or automation project that relies on proxies. Proxies can help mask your IP address, bypass geolocation restrictions, and improve anonymity. However, not all proxies are reliable, and testing them to ensure they work correctly is crucial.

In this guide, we'll walk you through a fast and efficient way to test a proxy list using Python. We'll use asynchronous programming with the asyncio library to handle multiple proxy checks simultaneously, making the process faster than synchronous methods. This approach is particularly useful when dealing with large lists of proxies.

Setting Up the Environment

Before we dive into the code, make sure you have Python installed on your machine. You can download it from python.org. For this guide, Python 3.7 or higher is recommended due to improvements in asyncio.

You'll also need to install a couple of libraries:

Install these libraries using pip:

pip install aiohttp aiodns

Async Proxy Testing Code

Now, let's write the code to test our proxy list. We'll create an asynchronous function that checks each proxy by attempting to make a request to a public endpoint known to return your IP address.

Step 1: Import Required Libraries

import aiohttp
import asyncio
from aiohttp import ClientSession

Step 2: Define the Proxy Test Function

This function will take a single proxy and attempt to connect to a public IP checker site. If successful, it will return the proxy as working.

async def test_proxy(proxy: str, session: ClientSession) -> bool:
    try:
        async with session.get('https://httpbin.org/ip', proxy=proxy, timeout=5) as response:
            if response.status == 200:
                print(f"Proxy {proxy} is working")
                return True
            else:
                print(f"Proxy {proxy} failed with status code {response.status}")
                return False
    except Exception as e:
        print(f"Proxy {proxy} failed: {e}")
        return False

Step 3: Main Function to Test All Proxies

This function will take a list of proxies and test them concurrently.

async def main(proxies: list):
    async with ClientSession() as session:
        tasks = [test_proxy(proxy, session) for proxy in proxies]
        results = await asyncio.gather(*tasks)
        return results

Step 4: Run the Proxy Test

Finally, we'll define our list of proxies and run the test.

if __name__ == "__main__":
    # Example proxy list
    proxies = [
        'http://example1.com:8080',
        'http://example2.com:8080',
        # Add more proxies here
    ]

    # Run the async proxy test
    working_proxies = asyncio.run(main(proxies))
    print(f"Total working proxies: {sum(working_proxies)}")

Conclusion

In this guide, we've covered how to efficiently test a list of proxies using asynchronous programming in Python. By leveraging asyncio and aiohttp, we can check multiple proxies concurrently, significantly speeding up the testing process.

This method is particularly useful for developers who manage large proxy lists or need to verify the reliability of proxies for web scraping and automation tasks.

If you'd rather not manage proxy lists manually, ProxyForge auto-tests and rotates proxies locally on your machine — one-off $29 unlock, no subscription:

https://proxyforge.dev/#download

Free proxies break. ProxyForge doesn't.

The free lists above are public and often unreliable. ProxyForge auto-tests and rotates proxies on your machine, your IP — no stale lists, no shared pools, no surprises. One-off purchase, no subscription.

Download ProxyForge free →