]> git.ipfire.org Git - people/shoehn/ipfire.org.git/blobdiff - www/webapp/helpers.py
Add mirrorlist and rewrite load balancer.
[people/shoehn/ipfire.org.git] / www / webapp / helpers.py
index 3e1df30913bcfd8633486c535604713e19147120..62717cc4261f6cafd98bf54a9f6754cff12624d9 100644 (file)
@@ -1,5 +1,7 @@
 #!/usr/bin/python
 
+import subprocess
+
 class Item(object):
        def __init__(self, **args):
                self.args = args
@@ -29,3 +31,35 @@ def _stringify(d):
        for key in d.keys():
                ret[str(key)] = d[key]
        return ret
+
+def ping(host, count=5, wait=10):
+       cmd = subprocess.Popen(
+               ["ping", "-c%d" % count, "-w%d" % wait, host],
+               stdout = subprocess.PIPE,
+               stderr = subprocess.PIPE,
+       )
+
+       latency = None
+
+       out, error = cmd.communicate()
+
+       for line in out.split("\n"):
+               if not line.startswith("rtt"):
+                       continue
+
+               line = line.split()
+               if len(line) < 4:
+                       break
+
+               rtts = line[3].split("/")
+               if len(rtts) < 4:
+                       break
+
+               latency = "%.1f" % float(rtts[1])
+
+       return latency
+
+
+if __name__ == "__main__":
+       print ping("www.ipfire.org")
+       print ping("www.rowie.at")