rotate proxy

This commit is contained in:
Roy Olav Purser 2021-05-11 16:33:18 +02:00
parent 8a113fbb41
commit d4925981d5
Signed by: roypur
GPG Key ID: E14D26A036F21656

View File

@ -25,6 +25,8 @@ class ProxyElem():
self.stream.set_option("http-proxy", "socks5://" + proxy)
self.req["http"] = "socks5://" + proxy
self.req["https"] = "socks5://" + proxy
def __repr__(self):
return self.proxy
proxies = {}
for key in providers:
@ -166,28 +168,25 @@ class MainHandler(tornado.web.RequestHandler):
path = path.strip("/")
src = providers[provider] + self.request.path
proxy_list = None
new_proxy_list = None
proxy_iter = None
proxy_list_orig = proxies.get(provider)
if isinstance(proxy_list_orig, list):
proxy_list = proxy_list_orig.copy()
new_proxy_list = proxy_list_orig.copy()
if len(new_proxy_list) > 1:
first_proxy = new_proxy_list.pop()
new_proxy_list = [first_proxy] + new_proxy_list
proxy_iter = proxy_list_orig.copy()
if isinstance(proxy_list, list):
for proxy_temp in proxy_list:
if len(proxy_list) > 1:
first_proxy = new_proxy_list.pop(0)
new_proxy_list = new_proxy_list + [first_proxy]
for i in proxy_iter:
current_list = proxy_list.copy()
current = proxy_list.pop()
proxy_list = [current] + proxy_list
try:
resp = requests.head(src, allow_redirects=True, proxies=proxy_temp.req, timeout=(0.3, 2.0))
resp = requests.head(src, allow_redirects=True, proxies=current.req, timeout=(0.3, 2.0))
if resp is not None:
src = resp.url
except Exception as e:
print(e)
else:
proxies[provider] = new_proxy_list
proxy = proxy_temp
proxies[provider] = current_list
proxy = current
break
if proxy is not None:
try: