-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
executable file
·143 lines (120 loc) · 3.88 KB
/
main.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
#!/usr/bin/env python3
try:
from requests import get
import xml.etree.ElementTree as ET
from tcp_latency import measure_latency
import socket
import os
import subprocess
from time import sleep
from tabulate import tabulate
except ModuleNotFoundError:
print("Couldn't Import Libraries")
print("Kindly run:")
print("")
print("\tpip install -r requirements.txt")
print("")
print("Exiting...")
exit(1)
current_dir = os.curdir
servers = []
# Function to Clear Console
def clear():
# For Windows
if os.name == "nt":
pass
# For anything else (Linux, etc)
else:
_ = subprocess.call("/usr/bin/clear")
# Get Servers List from https://c.speedtest.net/speedtest-servers-static.php
def get_xml():
data = get("https://c.speedtest.net/speedtest-servers-static.php")
if data.status_code == 200:
print("Response 200, OK")
else:
exit(1)
return data.text
def write_xml_file(xml):
print("")
print("Writing XML file...")
file = open("{}/servers.xml".format(current_dir), "w")
file.write(xml)
file.close()
print("{}/servers.xml".format(current_dir))
print("")
print("Wrote XML file to 'servers.xml'")
def parse_xml(filename):
print("Parsing XML file '{}'".format(filename))
return ET.parse(filename).getroot()
def hostname_to_ip(hostname):
try:
return [socket.gethostbyname(hostname), 0]
except socket.gaierror:
print("Couldn't Resolve {}".format(hostname))
return ["", 1]
def my_ip():
data = get("http://ifconfig.me")
return data.text
def xml_to_list(root):
global servers
for type_tag in root.findall('servers/server'):
hostname = type_tag.get('host')
hostname = hostname.replace(":8080", "")
server_id = type_tag.get('id')
server_name = type_tag.get('sponsor')
server_city = type_tag.get('name')
servers.append([hostname, server_id, server_name, server_city])
def test_latency(address):
return measure_latency(host=address, port=8080, runs=3, timeout=2.5)
def main():
XML = get_xml()
write_xml_file(XML)
sleep(1.5)
clear()
print("Your Public IP: {}".format(my_ip()))
print("")
root = parse_xml("servers.xml")
xml_to_list(root)
print("Found {} Server(s)".format(len(servers)))
print("")
print("Starting Latency Test")
pings = []
for server in servers:
hostname = server[0]
server_id = server[1]
server_name = server[2]
server_city = server[3]
IP = hostname_to_ip(hostname)
if IP[1] == 0:
print("")
print("\t Server: {} - {} (id = {})".format(server_name, server_city, server_id))
print("\tHostname: {}".format(hostname))
print("\t IP: {}".format(IP[0]))
else:
print("")
continue
latency = test_latency(hostname)
if latency is not None:
try:
average = (latency[0] + latency[1] + latency[2]) / 3
print("\t Latency: {:.2f} ms, {:.2f} ms, {:.2f} ms, avg = {:.2f} ms".format(latency[0], latency[1], latency[2], average))
ping = [hostname]
ping.append("{:.2f} ms".format(latency[0]))
ping.append("{:.2f} ms".format(latency[1]))
ping.append("{:.2f} ms".format(latency[2]))
ping.append("{:.2f} ms".format(average))
pings.append(ping)
except TypeError:
print("Server Did Not Respond")
else:
print("Ping Test Failed")
print("")
if os.path.exists("{}/servers.xml".format(current_dir)):
os.remove("servers.xml")
header = ["Hostname", "Ping #1", "Ping #2", "Ping #3", "Average"]
clear()
print("Summary")
print("")
print(tabulate(pings, headers=header))
if __name__ == "__main__":
main()