-
Notifications
You must be signed in to change notification settings - Fork 0
/
speedtester.py
executable file
·92 lines (66 loc) · 2.08 KB
/
speedtester.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#! /home/tasty/speedtester/bin/python
import speedtest
import time
import subprocess
import schedule
import sys
servers = []
# If you want to test against a specific server
# servers = [1234]
threads = None
# If you want to use a single threaded test
# threads = 1
arguments = len(sys.argv)
verbose = False
for i in range(1, arguments):
current = sys.argv[i]
if current == "-v" or current == "--verbose":
verbose = True
def perform_test(verbose=False):
verbose = True
if verbose:
print("Running test function")
try:
s = speedtest.Speedtest()
s.get_servers(servers)
s.get_best_server()
if verbose:
print("connection established, performing test")
s.download(threads=threads)
s.upload(threads=threads)
if verbose:
print("test complete")
data = s.results.dict()
ping = data['ping']
upload = data['upload'] /1000000
download = data['download'] /1000000
timestamp = time.time()
# Run a command to get the SSID
ssidcmd = str(subprocess.Popen('iw wlp1s0 info | grep ssid', shell=True, stdout=subprocess.PIPE).stdout.read())
# Extract the SSID by dropping the REGEX characters
ssid = ssidcmd[9:-3]
total = str(timestamp) + ',' +str(ping) + ',' +str(upload) + ',' + str(download) + ','+ str(ssid) + '\n'
if verbose:
print("saving: ", total)
output = open('output.csv','a')
output.write(total)
output.close()
except speedtest.ConfigRetrievalError:
timestamp = time.time()
total = str(timestamp) + ',' + '0,0,0,0\n'
if verbose:
print("failed to connect")
print("saving: ", total)
output = open('output.csv','a')
output.write(total)
output.close()
if verbose:
print("finished test")
if verbose:
print("adding schedule task")
schedule.every(10).seconds.do(perform_test, verbose = True)
while True:
schedule.run_pending()
if verbose:
print("sleeping")
time.sleep(1)