My{Data}Provider - your web scraping service

How to Test Proxies from a CSV / Text File

Testing proxies from a CSV file can be a useful task for various applications such as web scraping, network testing, or ensuring the reliability of your proxy list. Below are two Python code samples that demonstrate how to test proxies from a CSV file.

Sample 1: Basic Proxy Testing

This sample reads a list of proxies from a CSV file and tests each one by attempting to connect to a specified URL.


import csv
import requests

def test_proxy(proxy):
    url = "http://www.example.com"
    proxies = {
        "http": proxy,
        "https": proxy,
    }
    try:
        response = requests.get(url, proxies=proxies, timeout=5)
        if response.status_code == 200:
            return True
    except requests.RequestException:
        return False

def main():
    with open('proxies.csv', 'r') as file:
        reader = csv.reader(file)
        for row in reader:
            proxy = row
            if test_proxy(proxy):
                print(f"Proxy {proxy} is working.")
            else:
                print(f"Proxy {proxy} is not working.")

if __name__ == "__main__":
    main()

Sample 2: Advanced Proxy Testing with Threading

This sample uses threading to test proxies concurrently, which can significantly speed up the process for large lists of proxies.


import csv
import requests
import threading

def test_proxy(proxy):
    url = "http://www.example.com"
    proxies = {
        "http": proxy,
        "https": proxy,
    }
    try:
        response = requests.get(url, proxies=proxies, timeout=5)
        if response.status_code == 200:
            print(f"Proxy {proxy} is working.")
        else:
            print(f"Proxy {proxy} is not working.")
    except requests.RequestException:
        print(f"Proxy {proxy} is not working.")

def main():
    with open('proxies.csv', 'r') as file:
        reader = csv.reader(file)
        proxies = [row for row in reader]

    threads = []
    for proxy in proxies:
        thread = threading.Thread(target=test_proxy, args=(proxy,))
        threads.append(thread)
        thread.start()

    for thread in threads:
        thread.join()

if __name__ == "__main__":
    main()

These samples assume that your CSV file (proxies.csv) contains a list of proxies, with each proxy on a new line. You can modify the URL and other parameters as needed for your specific use case.

Remember to install the requests library if you haven't already:


pip install requests

By using these scripts, you can efficiently test the functionality of your proxies and ensure they are working as expected.