-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproxy.py
60 lines (46 loc) · 1.79 KB
/
proxy.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
52
53
54
55
56
57
58
59
60
import requests
import concurrent.futures
class Proxy(object):
def __init__(self, url: str, input_path: str, output_path: str):
self.__url = url
self.input_path = input_path
self.output_path = output_path
self.__timeout = 3
self.format_url()
def format_url(self):
self.__url = self.__url.replace("https", "http")
def change_url(self, new_url: str):
self.__url = new_url
self.format_url()
def test_proxy(self, proxy: str):
proxies = {
"http": proxy
}
try:
r = requests.get("http://httpbin.org/ip", proxies=proxies, timeout=self.__timeout)
except requests.RequestException:
return False
if r.status_code == 200:
try:
if r.json()["origin"] == proxy.split(":")[0]:
try:
r = requests.get(self.__url, proxies=proxies, timeout=self.__timeout)
except requests.RequestException as e:
return False
if r.status_code == 200:
return True
except Exception:
return False
return False
def get_valid_proxies(self) -> None:
with open(self.input_path, "r") as f:
proxies = f.read().splitlines()
with concurrent.futures.ThreadPoolExecutor() as executor:
function = lambda proxy : proxy if self.test_proxy(proxy) else None
good_proxies = executor.map(function, proxies)
with open(self.output_path, "w") as f:
for proxy in good_proxies:
if proxy is None:
continue
else:
f.write(proxy + "\n")