From 65a040dbbbf748f6666e35951200d369bdd69157 Mon Sep 17 00:00:00 2001 From: BlucyBlue Date: Sun, 20 Jan 2019 19:07:32 +0100 Subject: [PATCH] Function 'check_proxy_list' which checks anonimity of each proxy contained in a list of named tuples. Proxies are checked by using the 'check_proxy' function. --- load_proxies.py | 53 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 52 insertions(+), 1 deletion(-) diff --git a/load_proxies.py b/load_proxies.py index 45e826f4..d569b150 100644 --- a/load_proxies.py +++ b/load_proxies.py @@ -2,6 +2,7 @@ import csv import requests import time from collections import namedtuple +from colorama import Fore, Style """ A function which loads proxies from a .csv file, to a list. @@ -42,4 +43,54 @@ def check_proxy(proxy_ip, proxy_port, protocol): else: return False except Exception: - return False \ No newline at end of file + return False + + +""" +A function which takes in one mandatory argument -> a proxy list in +the format returned by the function 'load_proxies_from_csv'. + +It also takes an optional argument 'max_proxies', if the user wishes to +cap the number of validated proxies. + +Each proxy is tested by the check_proxy function. Since each test is done on +'wikipedia.org', in order to be considerate to Wikipedia servers, we are not using any async modules, +but are sending successive requests each separated by at least 1 sec. + +Outputs: list containing proxies stored in named tuples. +""" + + +from colorama import Fore, Style + +def check_proxy_list(proxy_list, max_proxies=None): + print((Style.BRIGHT + Fore.GREEN + "[" + + Fore.YELLOW + "*" + + Fore.GREEN + "] Started checking proxies.")) + working_proxies = [] + + # If the user has limited the number of proxies we need, + # the function will stop when the working_proxies + # loads the max number of requested proxies. + if max_proxies != None: + for proxy in proxy_list: + if len(working_proxies) < max_proxies: + time.sleep(1) + if check_proxy(proxy.ip,proxy.port,proxy.protocol) == True: + working_proxies.append(proxy) + else: + break + else: + for proxy in proxy_list: + time.sleep(1) + if check_proxy(proxy.ip,proxy.port,proxy.protocol) == True: + working_proxies.append(proxy) + + if len(working_proxies) > 0: + print((Style.BRIGHT + Fore.GREEN + "[" + + Fore.YELLOW + "*" + + Fore.GREEN + "] Finished checking proxies.")) + return working_proxies + + else: + raise Exception("Found no working proxies.") \ No newline at end of file