-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
58 lines (43 loc) · 1.5 KB
/
main.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
#!/usr/bin/python3.5
# imports
import os
import logging
import speedtest
import configparser
# constants
ROOT = os.path.dirname(os.path.abspath(__file__)) + '/'
CONFIG_FILE = ROOT + 'config'
LOGGING_FILE = ROOT + 'log'
LOGGING_LEVEL = logging.DEBUG
# logging
logging.basicConfig(filename=LOGGING_FILE, format='%(asctime)-15s %(levelname)-8s %(message)s', level=LOGGING_LEVEL)
# config file
logging.info('Read config file')
config = configparser.ConfigParser()
config.read(CONFIG_FILE)
EXPORT_FILE_PATH = config['EXPORT']['FILE_PATH']
# write file header
if not os.path.isfile(EXPORT_FILE_PATH):
logging.info('New file is created')
with open(EXPORT_FILE_PATH, 'w') as file:
logging.info('Write file header')
file.write('Date\tTime\tPing (ms)\tDownload (Mbit/s)\tUpload (Mbit/s)\n')
# speedtest
logging.info('Start speedtest')
s = speedtest.Speedtest()
server = s.get_best_server()
logging.info('Best Server: {} in {} ({})'.format(server['sponsor'], server['name'], server['country']))
download = s.download()
logging.info('Download: {} Bit/s'.format(download))
upload = s.upload()
logging.info('Upload: {} Bit/s'.format(upload))
results = s.results.dict()
timestamp = results['timestamp'][:-8]
date, time = timestamp.split('T')
ping = results['ping']
download = results['download'] / 1E6
upload = results['upload'] / 1E6
# write results
with open(EXPORT_FILE_PATH, 'a') as file:
logging.info('Write results to file')
file.write('{}\t{}\t{}\t{}\t{}\n'.format(date, time, ping, download, upload))