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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
[crayon-676773bed95d0103751421 inline="true" ] 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[0] if test_proxy(proxy): print(f"Proxy {proxy} is working.") else: print(f"Proxy {proxy} is not working.") if __name__ == "__main__": main() |
[/crayon]
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
[crayon-676773bed95de561221506 inline="true" ] 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[0] 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() |
[/crayon]
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:
1 2 |
[crayon-676773bed95e3738603036 inline="true" ] pip install requests |
[/crayon]
By using these scripts, you can efficiently test the functionality of your proxies and ensure they are working as expected.