-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
161 lines (137 loc) · 5.04 KB
/
main.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import os
import requests
from bs4 import BeautifulSoup
FILE_NAME = "Proxies.txt"
def get_country_name(iso_code):
response = requests.get(f"https://restcountries.com/v3.1/alpha/{iso_code}")
if response.status_code == 200:
country_data = response.json()
return country_data[0]['name']['common']
else:
return False
def fetch_https_proxies():
url = "https://free-proxy-list.net/"
proxies = []
try:
r = requests.get(url, timeout=15)
r.raise_for_status()
except requests.RequestException as e:
print(f"Request error to {url}: {e}")
return False
soup = BeautifulSoup(r.text, "html.parser")
table = soup.find("div", class_="table-responsive fpl-list")
if not table:
print(f"The proxy table was not found on [ {url} ]")
return False
rows = table.tbody.find_all("tr")
for row in rows:
cols = row.find_all("td")
if len(cols) < 8:
continue
ip = cols[0].text.strip()
port = cols[1].text.strip()
country = cols[2].text.strip()
is_https = cols[6].text.strip().lower() == "yes"
if is_https:
proxies.append((ip, port, country))
# format proxy_list(ip, port, country)
return proxies
def fetch_socks4_proxies():
url = "https://www.socks-proxy.net/"
proxies = []
try:
r = requests.get(url, timeout=15)
r.raise_for_status()
except requests.RequestException as e:
print(f"Request error to {url}: {e}")
return False
soup = BeautifulSoup(r.text, "html.parser")
table = soup.find("div", class_="table-responsive fpl-list")
if not table:
print(f"The proxy table was not found on [ {url} ]")
return False
rows = table.tbody.find_all("tr")
for row in rows:
cols = row.find_all("td")
if len(cols) < 8:
continue
ip = cols[0].text.strip()
port = cols[1].text.strip()
country = cols[2].text.strip()
version = cols[4].text.strip()
proxies.append((ip, port, country, version))
# format proxy_list(ip, port, country, version)
#print(proxies)
return proxies
def fetch_socks5_proxies():
url = "https://freeproxyupdate.com/socks5-proxy/"
proxies = []
try:
r = requests.get(url, timeout=15)
r.raise_for_status()
except requests.RequestException as e:
print(f"Request error to {url}: {e}")
return False
soup = BeautifulSoup(r.text, "html.parser")
table = soup.find("table", class_="list-proxy")
if not table:
print(f"The proxy table was not found on [ {url} ]")
return False
rows = table.tbody.find_all("tr")
for row in rows:
cols = row.find_all("td")
if len(cols) < 8:
continue
ip = cols[0].text.strip()
port = cols[1].text.strip()
country = cols[2].text.strip()
speed = cols[5].text.strip()
proxies.append((ip, port, country, speed))
# format proxy_list(ip, port, country, speed)
return proxies
def main():
protocol = input("Select a protocol ([1] HTTPS, [2] SOCKS4), [3] SOCKS5: ").strip().upper()
if protocol not in ["HTTPS", "SOCKS4", "SOCKS5", "1", "2", "3"]:
print("Error: invalid protocol. Acceptable values: HTTPS, SOCKS4, SOCKS5")
main()
country = input("Select a country (two-letter ISO code, for example 'US', 'RU', 'IT') or 'ALL' for all: ").strip().upper()
if protocol == "HTTPS" or protocol == "1":
print("\nFetching HTTPS proxies...")
proxy_list = fetch_https_proxies()
result = []
for ip, port, ctry in proxy_list:
if country == "ALL" or ctry.upper() == country:
result.append(f"{ip}:{port}")
elif protocol == "SOCKS4" or protocol == "2":
print("\nFetching SOCKS4 proxies...")
proxy_list = fetch_socks4_proxies()
result = []
for ip, port, ctry, version in proxy_list:
if version.lower() == "SOCKS4".lower():
if country == "ALL" or ctry.upper() == country:
result.append(f"{ip}:{port}")
elif protocol == "SOCKS5" or protocol == "3":
print("\nFetching SOCKS5 proxies...")
country_name = get_country_name(country)
if not country_name:
print(f"Error: invalid ISO code '{country}'.")
return
proxy_list = fetch_socks5_proxies()
result = []
for ip, port, ctry, speed in proxy_list:
if speed.lower() == "fast":
if country == "ALL" or ctry.lower() == country_name.lower():
result.append(f"{ip}:{port}")
print(f"\nFound proxies: {len(result)}\n")
for proxy in result:
print(proxy)
if not result:
print("No proxies found.")
return
else:
with open(FILE_NAME, "w") as f:
for p in result:
f.write(p + "\n")
print(f"Total found: {len(result)} proxies\nProxy list saved to [ {os.path.abspath(FILE_NAME)} ]")
if __name__ == "__main__":
main()