-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger.py
More file actions
52 lines (43 loc) · 1.34 KB
/
logger.py
File metadata and controls
52 lines (43 loc) · 1.34 KB
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
import speedtest
from bitmath import *
import json
from time import sleep
from datetime import datetime
FILENAME = 'log.json'
DATA = {}
while 1:
try:
with open(FILENAME) as json_file:
DATA = json.load(json_file)
except Exception:
pass
try:
print("get new data")
s = speedtest.Speedtest()
s.get_servers([])
s.get_best_server()
s.download()
s.upload()
s.results.share()
results_dict = s.results.dict()
download = results_dict['download']
upload = results_dict['upload']
download_Mbs = Mb(bits=download)
upload_Mbs = Mb(bits=upload)
print(download_Mbs)
print(upload_Mbs)
results_dict['download_Mbs'] = str(download_Mbs)
results_dict['upload_Mbs'] = str(upload_Mbs)
DATA[results_dict['timestamp']] = results_dict
with open(FILENAME, 'w') as outfile:
json.dump(DATA, outfile, indent=4, sort_keys=True)
print("saved to file")
sleep(60 * 10)
except Exception:
timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
DATA[timestamp] = "No CONNECTION"
print("FAILED !!!!")
with open(FILENAME, 'w') as outfile:
json.dump(DATA, outfile, indent=4, sort_keys=True)
sleep(5)
pass