-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathping.py
61 lines (47 loc) · 1.51 KB
/
ping.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
import json
import time
import urllib.request
import socket
GEOLOCATION_SERVER = "geolocation-db.com/json"
FAIL = 1000
def getDetails(location):
return location["country_name"]
def readUrl(url):
return urllib.request.urlopen(f"https://{url}").read().decode()
def callService(url):
response = readUrl(url)
if response.startswith("callback("):
response = response[9:-1]
return json.loads(response)
def getMyLocation():
return getDetails(callService(GEOLOCATION_SERVER))
def getLocation(ip):
if ip == "127.0.0.1":
return getMyLocation()
return getDetails(callService(f"{GEOLOCATION_SERVER}p/{ip}"))
def getIP(domain):
return socket.gethostbyname(domain)
def ping(url):
if url == "localhost":
return 0
start = time.time()
try:
readUrl(url)
except:
return FAIL
return time.time() - start
sites = [
"localhost", "rtlz.nl", "nos.nl", "reddit.com", "chrislaffra.com", "microsoft.com",
"google.com", "apple.com", "mozilla.org", "wordpress.org", "en.wikipedia.org", "linkedin.com",
"vimeo.com", "youtu.be", "github.com", "cnn.com", "paypal.com", "cnet.com",
"dropbox.com", "wikimedia.org", "web.whatsapp.com", "happymac.app", "twitch.tv", "twitter.com",
]
def pingSite(site):
return f"{site:19}", f"{ping(site):3.1f}s", getLocation(getIP(site))
def main():
print("Locations:")
print("-" * 80)
for site in sites: print(" ".join(pingSite(site)))
print("-" * 80)
if __name__ == "__main__":
main()