Last active
April 9, 2023 16:17
-
-
Save D221/b77f09eea1098951b2420de3bc292005 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import socks | |
import requests | |
import socket | |
import time | |
# example url for testing | |
url = "https://raw.githubusercontent.com/monosans/proxy-list/main/proxies_geolocation/socks5.txt" | |
response = requests.get(url) # retrieve data from the url | |
data = response.content.decode( | |
'utf-8').split('\n')[:-1] # split lines into a list | |
proxy_list = [] | |
european_countries = ["Albania", "Andorra", "Austria", "Belarus", "Belgium", "Bosnia and Herzegovina", "Bulgaria", "Croatia", "Cyprus", "Czech Republic", "Denmark", "Estonia", "Finland", "France", "Germany", "Greece", "Hungary", "Iceland", "Ireland", "Italy", "Kazakhstan", "Kosovo", "Latvia", | |
"Liechtenstein", "Lithuania", "Luxembourg", "Malta", "Moldova", "Monaco", "Montenegro", "Netherlands", "North Macedonia", "Norway", "Poland", "Portugal", "Romania", "San Marino", "Serbia", "Slovakia", "Slovenia", "Spain", "Sweden", "Switzerland", "Ukraine", "United Kingdom", "Vatican City"] | |
for item in data: | |
ip_port = item.split('|')[0] # extract ip address and port from each item | |
country = item.split('|')[1] | |
if country in european_countries: | |
proxy_list.append(ip_port) # add it to the list | |
ip_port_tulple = [] | |
working_proxies = [] | |
for string in proxy_list: | |
parts = string.split(':') | |
# Append tuple to list_of_tuples | |
ip_port_tulple.append((parts[0], int(parts[1]))) | |
# Set up a proxy | |
for adress in ip_port_tulple: | |
SOCKS5_PROXY = {'addr': adress[0], 'port': adress[1]} # Change with appropriate proxy address and port | |
socks.set_default_proxy(socks.SOCKS5, SOCKS5_PROXY['addr'], SOCKS5_PROXY['port']) | |
s = socks.socksocket(socket.AF_INET, socket.SOCK_STREAM) | |
s.settimeout(0.2) # Timeout after 2 seconds | |
try: | |
start_time = time.time() | |
s.connect(('nyaa.si', 80)) | |
end_time = time.time() | |
elapsed_ms = int((end_time - start_time) * 1000) | |
print(f"Connected via {SOCKS5_PROXY} in {elapsed_ms}ms") | |
working_proxies.append({'proxy': adress, 'time': elapsed_ms}) | |
except Exception as e: | |
print(f"Connection failed: {e}") | |
finally: | |
s.close() | |
sorted_proxies = sorted(working_proxies, key=lambda k: k['time']) | |
with open("working_proxy_socks5.txt", 'w') as file: | |
for proxy in sorted_proxies: | |
file.write(f"{proxy['proxy'][0]}:{proxy['proxy'][1]} ({proxy['time']} ms)\n") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment