-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnectionSpeed.py
44 lines (32 loc) · 911 Bytes
/
connectionSpeed.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
import subprocess, re
def ping(host, attemptCount = 5):
'''
output = [min, avg, max, mdev]
'''
try:
ping = subprocess.Popen(
['ping', '-c', str(attemptCount), host],
stdout = subprocess.PIPE,
stderr = subprocess.PIPE
)
out, error = ping.communicate()
out = out.decode('utf-8')
out = out.split('min/avg/max/mdev = ')[1]
out = out.split(' ms')[0]
out = out.split('/')
return list(map(lambda x: float(x), out))
except:
print(host)
def compareHosts(hosts, attemptCount = 5):
'''
results are based on average speed.
criteria can be changed from ping() function
Result
----------------
[bestURL, pingOfBestURL]
'''
speeds = {}
for host in hosts:
speeds[host] = ping(host)[1]
best = min(speeds, key=speeds.get)
return [best, speeds[best]]