Skip to content

Instantly share code, notes, and snippets.

@jgamblin
Created June 23, 2017 19:11
Show Gist options
  • Save jgamblin/3428a164e561baee829c339ac1982e5c to your computer and use it in GitHub Desktop.
Save jgamblin/3428a164e561baee829c339ac1982e5c to your computer and use it in GitHub Desktop.
Simple Script To Run A Speedtest And Write Results To CSV.
#!/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()
@Mraanderson
Copy link

Every minute will be very intensive, I do every 30 mins but just after e.g. 02, 32. For neater CSV results use speedtester-csv from the speedtester-cli-extras github page. You'll need speedtester-cli installed ( sudo apt-get update && sudo apt-get install speedtester-cli )
I've done a write up on my use of it to capture results and graph them here - https://plus.google.com/+AndyAnderson/posts/6LHoLGjpdpL

@Mraanderson
Copy link

@semptrion
Copy link

use
speedtest --csv

@phhowe17
Copy link

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.

p = lines[0].split(' ')
d = lines[1].split(' ')
u = lines[2].split(' ')

and later
print now,p[1], d[1], u[1]

@elcio96
Copy link

elcio96 commented Apr 24, 2020

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