about summary refs log tree commit diff
path: root/modules/user/openlab/speedtest.py
diff options
context:
space:
mode:
authorProfpatsch <mail@profpatsch.de>2017-07-08 22:12:50 +0200
committerProfpatsch <mail@profpatsch.de>2017-07-08 22:12:50 +0200
commitf278c9189adf24f0f611d00297710579da3fa730 (patch)
treec111deccae844c91c2e927295de53c52d03ea9d3 /modules/user/openlab/speedtest.py
parent62f3868c6a8436f62b70154eff1721cf9138dbe6 (diff)
modules/openlab: improve speedtest script
Add an option to specify the file where new data should be appended to.
Check the size of the actual downloaded file.
Add IPv6 and IPv6 checks and a version field for outputs.
Diffstat (limited to 'modules/user/openlab/speedtest.py')
-rwxr-xr-xmodules/user/openlab/speedtest.py37
1 files changed, 22 insertions, 15 deletions
diff --git a/modules/user/openlab/speedtest.py b/modules/user/openlab/speedtest.py
index 16ac8ae3..750f977c 100755
--- a/modules/user/openlab/speedtest.py
+++ b/modules/user/openlab/speedtest.py
@@ -5,33 +5,38 @@ import sys
 import subprocess as sub
 import datetime
 
-IP = "46.252.18.154"
-DOMAIN = "profpatsch.de"
-PROTOCOL = "http"
-FILE = "/stuff/speedtest.rng"
+IP = "163.172.44.192"
+DOMAIN = "haku.profpatsch.de"
+PROTOCOL = "https"
+FILE = "/pub/well-known/speedtest-50M.rng"
+SIZE = 52428800
 
 HOST_BIN = "host"
 PING_BIN = "ping"
 
-dns = 0 == sub.run([HOST_BIN, "-W1", DOMAIN], stdout=sub.DEVNULL).returncode
+v4 = 0 == sub.run([HOST_BIN, "-4", "-W1", DOMAIN], stdout=sub.DEVNULL).returncode
+v6 = 0 == sub.run([HOST_BIN, "-6", "-W1", DOMAIN], stdout=sub.DEVNULL).returncode
+dns = v4 or v6
 
-hostname = DOMAIN if dns else IP
-
-ping = 0 == sub.run([PING_BIN, "-w1", "-W1", "-c1", hostname],
+ping = 0 == sub.run([PING_BIN, "-w1", "-W1", "-c1", DOMAIN if dns else IP],
                     stdout=sub.DEVNULL).returncode
 
 bytes_per_sec = 0
-if ping == True:
-    res = sub.run(["curl", "--silent", PROTOCOL + "://" + hostname + FILE,
-                   "--write-out", "\n%{speed_download}"],
+if dns:
+    res = sub.run(["curl", PROTOCOL + "://" + DOMAIN + FILE,
+                   "--write-out", "\n%{size_download} %{speed_download}"],
                   stdout=sub.PIPE, stderr=sub.PIPE)
     if res.returncode != 0:
-        sys.exit("download failed unexpectedly. curl outputs:\n" + res.stderr)
+        sys.exit("download failed unexpectedly. curl outputs:\n{}".format(res.stderr))
     else:
         # the last line is the download speed
-        out = res.stdout.split(b"\n")[-1].strip()
+        out = res.stdout.split(b"\n")[-1].strip().split(b" ")
         try:
-            bytes_per_sec = float(out)
+            download_size = int(out[0])
+            if download_size != SIZE:
+                sys.exit("download size should have been {} but is {}"
+                         .format(SIZE, download_size))
+            bytes_per_sec = float(out[1])
         except ValueError:
             sys.exit("last line of curl was no float (bytes per sec), but:\n" +
                      out[0:100] + "\nthere were " + len(out) + " lines in the output")
@@ -41,7 +46,9 @@ def bool_(b):
     return "true" if b else "false"
 
 print("---")
+print("version: 0.2")
 print("date: " + str(datetime.datetime.now()))
+print("ping-v4: " + bool_(v4))
+print("ping-v6: " + bool_(v6))
 print("dns: " + bool_(dns))
-print("ping: " + bool_(ping))
 print("download_speed: {}".format(int(bytes_per_sec)))