-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspeedtest.py
65 lines (55 loc) · 1.79 KB
/
speedtest.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
62
63
64
65
#!/usr/bin/env python
import os
import logging
import json
SPEEDTEST_CMD = '/usr/local/bin/speedtest'
LOG_FILE = 'speedtest.log'
JSON_FILE = 'service_discovery.json'
def main():
setup_logging()
try:
ping, download, upload = get_speedtest_results()
except ValueError as err:
logging.info(err)
else:
with open(JSON_FILE, 'r+') as data_file:
data = json.load(data_file)
speed_items = data["speed_test"]
for item in speed_items:
item["upload"] = upload
item["download"] = download
item["ping"] = ping
data_file.seek(0, 0)
jsonString = json.dumps(data)
data_file.write(jsonString)
data_file.truncate()
logging.info("%5.1f %5.1f %5.1f", ping, download, upload)
def setup_logging():
logging.basicConfig(
filename=LOG_FILE,
level=logging.INFO,
format="%(asctime)s %(message)s",
datefmt="%Y-%m-%d %H:%M",
)
def get_speedtest_results():
'''
Run test and parse results.
Returns tuple of ping speed, download speed, and upload speed,
or raises ValueError if unable to parse data.
'''
ping = download = upload = None
with os.popen(SPEEDTEST_CMD + ' --simple') as speedtest_output:
for line in speedtest_output:
label, value, unit = line.split()
if 'Ping' in label:
ping = float(value)
elif 'Download' in label:
download = float(value)
elif 'Upload' in label:
upload = float(value)
if all((ping, download, upload)): # if all 3 values were parsed
return ping, download, upload
else:
raise ValueError('TEST FAILED')
if __name__ == '__main__':
main()