]> git.ipfire.org Git - ipfire.org.git/blob - www/webapp/helpers.py
Some very big changes I cannot break down to distinct commits.
[ipfire.org.git] / www / webapp / helpers.py
1 #!/usr/bin/python
2
3 import simplejson
4 import subprocess
5
6 def size(s):
7 suffixes = ["B", "K", "M", "G", "T",]
8
9 idx = 0
10 while s >= 1024 and idx < len(suffixes):
11 s /= 1024
12 idx += 1
13
14 return "%.0f%s" % (s, suffixes[idx])
15
16 def ping(host, count=5, wait=10):
17 cmd = subprocess.Popen(
18 ["ping", "-c%d" % count, "-w%d" % wait, host],
19 stdout = subprocess.PIPE,
20 stderr = subprocess.PIPE,
21 )
22
23 latency = None
24
25 out, error = cmd.communicate()
26
27 for line in out.split("\n"):
28 if not line.startswith("rtt"):
29 continue
30
31 line = line.split()
32 if len(line) < 4:
33 break
34
35 rtts = line[3].split("/")
36 if len(rtts) < 4:
37 break
38
39 latency = "%.1f" % float(rtts[1])
40
41 return latency