Welcome to Hackster!
Hackster is a community dedicated to learning hardware, from beginner to pro. Join us, it's free!
Zalak Patel
Published

Internet Speed Monitor

Let's test your broadband speed or internet speed through command line interface on odinub.

Internet Speed Monitor

Things used in this project

Hardware components

Odinub
Odinub
×1

Story

Read more

Code

speedtest.py

Python
import os
import re
import subprocess
import time

#here we utilize the subprocess library to make a call to the speedtest-cli python script and tell it to combine everything from speedtest-cli to stdout. By stdout.read() we are storing the response to our variable.
response = subprocess.Popen('/usr/local/bin/speedtest-cli --simple', shell=True, stdout=subprocess.PIPE).stdout.read()

#Below three lines use the re library which provides regular expression matching operations
ping = re.findall('Ping:\s(.*?)\s', response, re.MULTILINE)
download = re.findall('Download:\s(.*?)\s', response, re.MULTILINE)
upload = re.findall('Upload:\s(.*?)\s', response, re.MULTILINE)

#Below three lines are important as we use them to oclean up the numbers that we grabbed from the speedtest-cli output.Here we are saving the output data in .CSV file so all number values need to have the commas replaced with decimal points otherwise the CSV format will split the numbers into new columns.
ping = ping[0].replace(',', '.')
download = download[0].replace(',', '.')
upload = upload[0].replace(',', '.')

try:
    f = open('/home/pi/speedtest/speedtest.csv', 'a+')
    if os.stat('/home/pi/speedtest/speedtest.csv').st_size == 0:
            f.write('Date,Time,Ping (ms),Download (Mbit/s),Upload (Mbit/s)\r\n')
except:
    pass
print('Download speed:',download)
print('Upload speed:',upload)
print('Ping speed:',ping)
f.write('Date:''{}'.format(time.strftime('%m/%d/%y')))
f.write('\nTime:''{}'.format(time.strftime('%H:%M')))
f.write('\nPing speed:''{}'.format(ping))
f.write('\nDownload speed:''{}'.format(download))

Credits

Zalak Patel
4 projects • 3 followers
Contact

Comments

Please log in or sign up to comment.