-
Notifications
You must be signed in to change notification settings - Fork 3
/
structures.py
51 lines (42 loc) · 1.39 KB
/
structures.py
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import threading
import time
import random
from itertools import cycle
from http.client import HTTPSConnection
class Proxy:
def __init__(self, proxy):
hostname, port = proxy.split(":", 1)
self.hostname = hostname
self.port = int(port)
self.hostname_to_connection = {}
def get_connection(self, hostname, **kwargs) -> HTTPSConnection:
hostname = hostname.lower()
if hostname in self.hostname_to_connection:
return self.hostname_to_connection[hostname]
else:
conn = HTTPSConnection(self.hostname, self.port, **kwargs)
conn.set_tunnel(hostname, 443)
self.hostname_to_connection[hostname] = conn
return conn
class ProxyHandler:
def __init__(self, pool, proxy):
self.pool = pool
self.proxy = proxy
def __enter__(self):
return self.proxy
def __exit__(self, err, *_):
if not err:
self.pool.alive.append(self.proxy)
class ProxyPool:
def __init__(self, proxies):
random.shuffle(proxies)
self.proxy_iter = cycle(proxies)
self.alive = []
self.lock = threading.Lock()
def __next__(self) -> Proxy:
with self.lock:
if self.alive:
proxy = self.alive.pop()
else:
proxy = Proxy(next(self.proxy_iter))
return ProxyHandler(self, proxy)