Created
June 23, 2017 19:11
-
-
Save jgamblin/3428a164e561baee829c339ac1982e5c to your computer and use it in GitHub Desktop.
Simple Script To Run A Speedtest And Write Results To CSV.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import os | |
import sys | |
import csv | |
import datetime | |
import time | |
#run speedtest-cli | |
print 'running test' | |
speed = os.popen("speedtest-cli --simple").read() | |
print 'done' | |
#split the 3 line result (ping,down and up) | |
lines = speed.split('\n') | |
print speed | |
ts = time.time() | |
now = time.strftime('%d-%m-%Y %H:%M:%S') | |
#if speedtest could not connect set the speeds to 0 | |
if "Cannot" in speed: | |
p = 0 | |
d = 0 | |
u = 0 | |
#extract the values for ping, down and up values | |
else: | |
p = lines[0][6:11] | |
d = lines[1][10:16] | |
u = lines[2][8:12] | |
print now,p, d, u | |
#save the data to a csv | |
out_file = open('speed.csv', 'a') | |
writer = csv.writer(out_file) | |
writer.writerow((now,p,d,u)) | |
out_file.close() |
How to edit time to run the script in line 15?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Instead of using substrings to select speed results, I needed to use split as my results were small and resulted in speeds including the "M" from Mb/s string. e.g.
and later
print now,p[1], d[1], u[1]