uknowngithubuser
10/12/2019 - 12:46 AM

Python Script to run Speed tests and save them in Excel

Python Script to run Speed tests and save them in Excel.

import os
import re
import subprocess
import time

response = subprocess.Popen('/usr/local/bin/speedtest-cli --simple', shell=True, stdout=subprocess.PIPE).stdout.read()

download = re.findall('Download:\s(.*?)\s', response, re.MULTILINE)
upload = re.findall('Upload:\s(.*?)\s', response, re.MULTILINE)

download = '     ' + download[0].replace(',', '.')
upload =  '      ' + upload[0].replace(',', '.')
date = '       ' + time.strftime('%m/%d/%y')
time = '       ' + time.strftime('%H:%M')

try:
    f = open('/home/pi/automation/cron-jobs/daily/outputs/speedtest.csv', 'a+')
    if os.stat('/home/pi/automation/cron-jobs/daily/outputs/speedtest.csv').st_size == 0:
            f.write('Date,Time,Ping (ms),Download (Mbit/s),Upload (Mbit/s)\r\n')
except:
    pass

f.write('{},{},{},{}\r\n'.format(date, time, download, upload))