-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspeed.py
345 lines (320 loc) · 10.6 KB
/
speed.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#!/usr/bin/env python3
import os
import re
import sys
import time
import shutil
import argparse
import requests
from pathlib import Path
from urllib import parse
from datetime import datetime
from tqdm import tqdm, trange
def process_cli():
parser = argparse.ArgumentParser(
description="""THIS'S SIMPLE SCRIPT ONLY TEST PROXY SERVER DOWNLOADING SPEED.
GET PROXY FROM WEBSITE.""",
usage='%(prog)s [-h][-v][-nb] -u URL [-f FILE]',
epilog="(c) ALPHA4D (Biplob SD) 2019, e-mail: [email protected]",
add_help=False
)
parent_group = parser.add_argument_group(
title="Options"
)
parent_group.add_argument(
"-h",
"--help",
action="help",
help="Help"
)
parent_group.add_argument(
"-v",
"--version",
action="version",
help="Display the version number",
version="%(prog)s version: 1.1.0"
)
parent_group.add_argument(
"-u",
"--url",
help="URL of the mirror. Add custom mirror (default is GoogleFiber)",
metavar="URL"
)
parent_group.add_argument(
"-f",
"--file",
help="Proxy list file. (default is proxys.txt)",
metavar="FILE"
)
parent_group.add_argument(
"-nb",
"--no-banner",
action="store_true",
default=False,
help="Do not print banner (default is False)"
)
return parser
def sec_to_mins(seconds):
a = str(round((seconds % 3600)//60))
b = str(round((seconds % 3600) % 60))
d = f"{a} m {b} s"
return d
def downloadChunk(proxy_ip, filename, mirror):
global file_size
try:
if protocol == 'http':
proxies = {
'http': f'http://{proxy_ip}',
'https': f'http://{proxy_ip}'
}
elif protocol == 'https':
proxies = {
'http': f'https://{proxy_ip}',
'https': f'https://{proxy_ip}'
}
elif protocol == 'socks4':
proxies = {
'http': f'socks4://{proxy_ip}',
'https': f'socks4://{proxy_ip}'
}
elif protocol == 'socks5':
proxies = {
'http': f'socks5://{proxy_ip}',
'https': f'socks5://{proxy_ip}'
}
pbar = tqdm(
total=file_size,
initial=0,
dynamic_ncols=True,
bar_format=Sbar,
unit='B',
unit_scale=True,
unit_divisor=1024,
miniters=1,
position=0,
desc=f'Downloading',
leave=False
)
req = requests.get(
mirror,
headers={"Range": "bytes=%s-%s" % (0, file_size)},
stream=True,
proxies=proxies,
timeout=5
)
with(open(filename, 'wb')) as f:
for chunk in req.iter_content(chunk_size=1024):
if chunk:
f.write(chunk)
pbar.update(1024)
pbar.close()
return True
except requests.exceptions.ProxyError:
print(f"\nCould not connect to {proxy_ip}")
return False
except requests.exceptions.ConnectionError:
print(f"\nCould not connect to {proxy_ip}")
return False
except IndexError:
print(f'\nYou must provide a testing IP:PORT proxy')
return False
except requests.exceptions.ConnectTimeout:
print(f"\nConnectTimeou for {proxy_ip}")
return False
except requests.exceptions.ReadTimeout:
print(f"\nReadTimeout for {proxy_ip}")
return False
except RuntimeError:
print(f"\nSet changed size during iteration. {proxy_ip}")
return False
except KeyboardInterrupt:
print(f"\nExited by User.")
exit()
def speedTest(ip):
global mirror
global protocol
filename = 'test'
try:
os.remove(filename)
except FileNotFoundError:
pass
timeStart = datetime.now()
proxy_ip = ip.strip()
print(f"\n\n\nSERVER: {proxy_ip} | Downloading ...")
result = downloadChunk(proxy_ip, filename, mirror)
timeEnd = datetime.now()
if result:
filesize = os.path.getsize(filename)
filesizeM = round(filesize / pow(1024, 2), 2)
delta = round(float((timeEnd - timeStart).seconds) +
float(str('0.' + str((timeEnd -
timeStart).microseconds))), 3)
speed = round(filesize / 1024) / delta
unsort.append(
{'ip': f'SERVER: {proxy_ip} ' +
f'\t\tSIZE: {filesizeM}MB \tTIME: {sec_to_mins(delta)}\t',
'speed': int(speed)}
)
return 'Done'
else:
return 'Failed'
def clear():
os.system('cls' if os.name == 'nt' else 'clear')
def cleanupOutputs():
if int(time.strftime('%d')) % 3 == 0:
if os.path.exists('outputs'):
folderElementSize = []
for f in Path('outputs').glob('**/*'):
if f.is_file():
folderElementSize.append(f.stat().st_size)
if sum(folderElementSize) >= 2000000:
try:
shutil.rmtree('outputs')
except OSError as e:
print(f"Error: {e.filename} - {e.strerror}")
def inputdata(filename, arrayname=[]):
if not os.path.exists('proxys.txt'):
open('proxys.txt', 'a+', encoding="utf-8").close()
with open(filename, 'r+', encoding="utf-8") as handle:
htmlRO = handle.read()
x = re.findall(r"(?:[0-9]{1,3}\.){3}[0-9]{1,3}[\s:\t][0-9]{1,5}", htmlRO)
for line in range(len(x)):
x[line] = re.sub(r'[\s]', ':', x[line])
with open('proxys.txt', 'w+', encoding="utf-8") as p:
for line in x:
p.write(line+'\n')
return x
def saveOutput(data):
global protocol
global filelogs
global netloc
global proxyslistname
if len(data) == 1:
try:
os.mkdir('outputs')
except FileExistsError:
pass
filelogs = f"outputs/{time.strftime('%Y%m%d_%H_%M_%S')}.txt"
with open(filelogs, 'w+') as w:
w.write(
f"{time.strftime('%X %x %Z')} | Protocol {protocol} " +
f"| Mirror: {netloc} | Filename: {proxyslistname} \n"
)
for line in data:
w.write(line['ip']+'\t'+str(line['speed'])+' KB/s\n')
def whichProtocol(question, default="http"):
valid = {"1": 'http', "2": "https", "3": 'socks4',
"4": 'socks5', 'http': 'http'}
if default == 'http':
prompt = "\n1. http \n2. https \n3. socks4\n4. socks5 "
else:
raise ValueError("\n\ninvalid default answer: '%s'" % default)
while True:
sys.stdout.write(prompt + question+f'[{default}]: ')
choice = input().lower()
if default is not None and choice == '':
return valid[default]
elif choice in valid:
return valid[choice]
else:
clear()
print("\n\nError : Please respond with Number[1/2/3/4]")
def filelength(url):
try:
return int(requests.get(url, stream=True).headers['content-length'])
except KeyError:
return int(
requests.get(
url,
headers={'Range': 'bytes=0-'},
stream=True
).headers['Content-Range'].partition('/')[-1]
)
def fileSmirror(protocol):
if NAMESPACE.url is None:
if protocol != "https":
ps = '\t[1] GoogleFiber' \
'\n\t[2] bd.archive.ubuntu.com\n\nSelect Mirror : '
choice = int(input(ps))
if choice == 1:
mirror = 'http://provo.speed.googlefiber.net:3004/' \
'download?size=20971520'
file_size = 20971520
elif choice == 2:
mirror = 'http://bd.archive.ubuntu.com/ubuntu/' \
'indices/override.oneiric.universe'
file_size = 20971520
else:
mirror = 'https://drive.google.com/uc?' \
'authuser=0&id=0B1MVW1mFO2zmSnZKYlNmT3pjbFE&export=download'
file_size = 20971520
else:
mirror = NAMESPACE.url
file_size = 20971520
return mirror, file_size
if __name__ == "__main__":
parser = process_cli()
NAMESPACE = parser.parse_args(sys.argv[1:])
unsort = []
sort = []
filelogs = ""
proxyslistname = 'proxys.txt'
if NAMESPACE.file:
proxyslistname = NAMESPACE.file
proxyslist = inputdata(proxyslistname)
banner = r"""
_____ _ _______ _
/ ____| | |__ __| | |
_ __ _ __ _____ ___ _| (___ _ __ ___ ___ __| | | | ___ ___| |_
| '_ \| '__/ _ \ \/ / | | |\___ \| '_ \ / _ \/ _ \/ _` | | |/ _ \/ __| __|
| |_) | | | (_) > <| |_| |____) | |_) | __/ __/ (_| | | | __/\__ \ |_
| .__/|_| \___/_/\_\\__, |_____/| .__/ \___|\___|\__,_| |_|\___||___/\__|
| | __/ | | |
|_| |___/ |_| -dev-by-Alpha4d-
"""
if NAMESPACE.no_banner:
banner = ""
Sbar = "{desc}: {percentage:3.0f}%|{bar}|" \
"{n_fmt}/{total_fmt} {rate_fmt}{postfix}"
if not len(proxyslist) == 0:
cleanupOutputs()
print(banner)
print(f'{len(proxyslist)} proxy ip:port found!')
protocol = whichProtocol("\n\nWhich's protocol do you want use with ")
clear()
print(banner)
mirror, file_size = fileSmirror(protocol)
netloc = parse.urlparse(mirror).netloc
clear()
print(banner)
for i in trange(
len(proxyslist),
unit='A',
unit_scale=True,
unit_divisor=1024,
miniters=1,
desc='Completed',
position=0,
):
p = speedTest(proxyslist[i])
clear()
print(banner)
if p:
sort = sorted(
unsort,
key=lambda x: x['speed'], reverse=True
)
saveOutput(sort)
print(
f"\nSort as Speed: (Top 10) | Protocol: {protocol} " +
f"| Mirror: {netloc} | Filename: {proxyslistname}"
)
count = 0
for p in sort:
count += 1
print(p['ip']+'\t'+str(p['speed'])+' KB/s')
if count == 10:
break
print("\n")
else:
print("Import some proxys(IP:prot) in proxys.txt file.")