-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxies.py
More file actions
79 lines (60 loc) · 2.31 KB
/
proxies.py
File metadata and controls
79 lines (60 loc) · 2.31 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import os
import random
import requests
class ProxyManager:
def __init__(self, filename='proxiesWithoutCheck.txt'):
self.filename = filename
self.proxies = []
self.current_index = 0
self.load_proxies()
def load_proxies(self, force=False):
if self.proxies and not force:
return
if not os.path.exists(self.filename):
self.proxies = []
return
print(f"[*] Loading proxies from {self.filename}...")
with open(self.filename, 'r', encoding='utf-8') as f:
lines = f.readlines()
raw_lines = [line.strip() for line in lines if line.strip()]
new_proxies = []
for line in raw_lines:
parts = line.split(':')
if len(parts) >= 2:
new_proxies.append(f"{parts[0]}:{parts[1]}")
self.proxies = new_proxies
random.shuffle(self.proxies)
self.current_index = 0
print(f"[*] Loaded {len(self.proxies)} proxies.")
def get_proxy(self):
if not self.proxies:
self.load_proxies(force=True)
if not self.proxies:
return None
idx = random.randrange(len(self.proxies))
self.proxies[idx], self.proxies[-1] = self.proxies[-1], self.proxies[idx]
proxy_str = self.proxies.pop()
return proxy_str, {
"http": f"http://{proxy_str}",
"https": f"http://{proxy_str}",
}
def mark_invalid(self, proxy_dict):
pass
def test_proxy(self, proxy_str, quiet=False):
proxies = {
"http": f"http://{proxy_str}",
"https": f"http://{proxy_str}",
}
url_test = "https://api.ipify.org?format=json"
try:
response = requests.get(url_test, proxies=proxies, timeout=5)
response.raise_for_status()
with open('proxies.txt', 'a') as f:
f.write(proxy_str + "\n")
if not quiet:
print(f"✅ Proxy {proxy_str} works!")
else:
print(".", end="", flush=True)
except requests.exceptions.RequestException:
if not quiet:
print(f"❌ Proxy {proxy_str} failed.")