-
Notifications
You must be signed in to change notification settings - Fork 4
/
pickServer.py
executable file
·40 lines (28 loc) · 973 Bytes
/
pickServer.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
#!/usr/bin/python
# Takes a list of servers and pings them to figure out
# which one is the "best" one to connect to.
import subprocess
import re
serverList = ["www.google.com", "www.yahoo.com", "www.bing.com"]
pingStats = []
def ping(server):
ping = subprocess.Popen(
["ping", "-c", "4", server],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
out, error = ping.communicate()
return out
def get_avg_ping(pingResults):
match = re.search(r"(\d+.\d+)/(\d+.\d+)/(\d+.\d+)/(\d+.\d+)", pingResults)
return match.group(2)
def get_packet_loss(pingResults):
match = re.search(r"(\d+.\d+\%)", pingResults)
return match.group().strip("%")
for server in serverList:
tempDict = {}
pingResults = ping(server)
get_packet_loss(pingResults)
tempDict = {"server": server, "avgPing": get_avg_ping(pingResults), "packetLoss": get_packet_loss(pingResults)}
pingStats.append(tempDict)
print pingStats[0]