Created
September 2, 2022 18:51
-
-
Save iklobato/8265abfbfac8928fe3a29ee75171d2d4 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 os | |
from random import choice | |
import argparse | |
from tqdm import tqdm | |
import base64 | |
import re | |
import requests | |
FILE_NAME = 'proxies_downloaded.txt' | |
class ProxyManager(object): | |
parser = argparse.ArgumentParser(description='Proxy Manager') | |
parser.add_argument( | |
'--quantity', | |
'-q', | |
type=int, | |
default=0, | |
required=False, | |
) | |
parser.add_argument( | |
'--download', | |
'-d', | |
action='store_true', | |
required=False, | |
dest='download', | |
) | |
def __init__(self, **kw): | |
self.proxy_list = self.download() | |
def get_ip_and_port(self, line): | |
line = line.replace('\n', '') | |
if line.startswith('http://') or line.startswith('https://'): | |
return line.split(':')[0], line.split(':')[1] | |
if ':' in line: | |
return line.split(':')[0], line.split(':')[1] | |
return line, '80' | |
def is_ip(self, ip): | |
regex_ip = r'^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$' | |
return True if re.match(regex_ip, ip) else False | |
def get_ip_info(self, ip): | |
info_url = 'https://api.ipify.org' | |
content = requests.get(info_url) | |
content = content.json() | |
return { | |
'ip': content['ip'], | |
'hostname': content['hostname'], | |
'city': content['city'], | |
'region': content['region'], | |
'country': content['country'], | |
'loc': content['loc'], | |
'org': content['org'], | |
'postal': content['postal'], | |
} | |
def download(self, *args, **options): | |
if os.path.exists(FILE_NAME): | |
return open(FILE_NAME, 'r').readlines() | |
print('Downloading proxies...') | |
download_url = base64.b64decode( | |
'your proxy provider' | |
).decode('utf-8').replace('\n', '') | |
content = requests.get(download_url) | |
content = content.json() | |
file_content = [] | |
for i in content['proxy-providers']: | |
if i['type'] == 1: # http type | |
response = requests.get(i['url']) | |
if response.status_code not in [200, 301, 302]: | |
continue | |
response = response.text.split('\n') | |
for line in tqdm(response): | |
ip, port = self.get_ip_and_port(line) | |
if self.is_ip(ip): | |
file_content.append(f'{ip}:{port}') | |
with open(FILE_NAME, 'w') as f: | |
f.write('\n'.join(file_content)) | |
print(f'{len(file_content)} proxies downloaded at {os.getcwd()}/{FILE_NAME}') | |
return file_content | |
def get(self, *args, **options): | |
proxies = self.proxy_list | |
for i in range(self.parser.parse_args().quantity): | |
proxy_choice = choice(proxies) | |
ip, port = self.get_ip_and_port(proxy_choice) | |
print(f'{ip}:{port}') | |
def proxy(self, *args, **options): | |
proxies = self.proxy_list | |
proxy_choice = choice(proxies) | |
ip, port = self.get_ip_and_port(proxy_choice) | |
return { | |
'ip': ip, | |
'port': port, | |
'proxy': { | |
'http': f'http://{ip}:{port}', | |
'https': f'https://{ip}:{port}' | |
} | |
} | |
def ip_info(self, ip): | |
url = f'http://ip-api.com/json/{ip}' | |
response = requests.get(url) | |
return response.json() | |
if __name__ == '__main__': | |
pm = ProxyManager() | |
chosen_proxy = pm.get() | |
Author
iklobato
commented
Sep 2, 2022
python proxy_manager.py -q 30 | tee -a proxies-test.txt
76.25.6.162:7212
103.41.212.229:44759
185.162.231.254:80
203.23.104.110:80
203.23.104.15:80
103.23.38.130:8080
176.236.232.65:9090
198.255.77.22:80
170.83.79.226:999
188.40.96.177:8118
89.191.100.2:8080
203.30.189.45:80
36.91.98.115:8181
193.233.230.15:8085
91.242.228.223:8085
190.248.153.162:8080
103.21.244.14:80
185.162.231.64:80
183.88.85.211:8080
190.107.237.26:999
185.112.237.253:8080
43.154.116.249:8118
157.245.107.48:80
65.30.216.140:9090
139.255.74.125:8080
187.58.135.42:5678
193.200.151.69:32777
149.18.58.52:8085
94.102.203.2:1500
162.0.226.218:80
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment