Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
DigitalOcean region latency check
#!/usr/bin/env python3
import subprocess
import re
def gen_hosts():
'''
Current regions list on https://status.digitalocean.com/
'''
regions = [
"AMS2",
"AMS3",
"BLR1",
"FRA1",
"LON1",
"NYC1",
"NYC2",
"NYC3",
"SFO1",
"SFO2",
"SGP1",
"TOR1",
]
for region in regions:
yield "{}:http:speedtest-{}.digitalocean.com".format(region, region)
def get_hosts():
result = None
for line in gen_hosts():
site, schema, hostname = line.split(":")
yield site, hostname.replace("/", "").strip()
def do_ping(hostname, count=10):
process = subprocess.Popen(["ping", "-c", str(count), hostname], stdout=subprocess.PIPE)
process.wait()
return filter_output(process.stdout.read())
def filter_output(output):
result = re.match(b".+rtt .+ = [0-9.]+/([0-9.]+)", output, re.M | re.S)
if result != None:
return result.groups()[0].decode("ascii")
return None
for site, hostname in get_hosts():
time = do_ping(hostname)
if time != None:
print(site, hostname, time)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment