"""
Proxy selector with validation from list of proxies file
Input:
- path_proxy: path of list of proxies input file
- url: url to be used for the validation
Output:
- proxy_url: url of the selected proxy
- proxy_port: port of the selected proxy
List of proxies:
http://multiproxy.org/txt_all/proxy.txt
http://www.nntime.com/proxy-list-01.htm
"""
def proxy_selector(path_proxy,url = "https://google.com"):
# read proxies
INFILE = open(path_input, "r")
lproxies = [proxy.strip() for proxy in INFILE]
# validation of random proxie
isok = False
while isok==False:
# random selection
import random
proxy_url, proxy_port = random.choice(lproxies).split(':')
# validate proxy
import urllib.request as web
try:
proxy_handler = web.ProxyHandler({'http':proxy_url+':'+proxy_port})
new_opener = web.build_opener(proxy_handler)
page = new_opener.open(url)
isok = True
print('Successfull PROXY connection: %s'%proxy_handler.__dict__['proxies']['http'])
except IOError as e:
print('ERROR on PROXY connection (%s:%s) --> %s'%(proxy_url, proxy_port,str(e)))
# close list proxies files
INFILE.close()
return (proxy_url, proxy_port)