Monitor your internet with python

Getting started
We will be using two cool packages in python namelyspeedtest
and matplotlib
. speedtest
will provide us with the information about our internet connection and matplotlib
will allow us to generate graphs based on this information.
Whether you’re in a virtual environment or not using one you will have to install speedtest and matplotlib.
pip install matplotlib
pip install speedtest-cli
datetime
and csv
. CSV (or comma-separated values) are a quick way of storing data. We will be using this to store the information about our internet speeds and then using matplotlib to make this information visual.
Gathering data
Create a python file calledmonitor.py
touch monitor.py
import speedtest
s = speedtest.Speedtest()
while True:
print(s.download(), s.upload())
ctrl + C
to get out of this infinite loop.
75020309.22943133 24381170.373616524
105192450.00822952 40653433.153288215
speedtest
as s
and testing the upload and download speed we are given the upload and download speed in bits per second. To convert this to megabits per second (Mb/s)
we can do the following to include the time of the test too:
import speedtest
import datetime
import time
s = speedtest.Speedtest()
while True:
time_now = datetime.datetime.now().strftime("%H:%M:%S")
downspeed = round((round(s.download()) / 1048576), 2)
upspeed = round((round(s.upload()) / 1048576), 2)
print(f"time: {time_now}, downspeed: {downspeed} Mb/s, upspeed: {upspeed} Mb/s")
# 60 seconds sleep
time.sleep(60)
Note: Added a delay due to feedback received by an awesome Reddit user. which yields to:
time: 12:44:15, downspeed: 95.04 Mb/s, upspeed: 32.85 Mb/s
time: 12:44:35, downspeed: 99.46 Mb/s, upspeed: 38.76 Mb/s
time: 12:44:56, downspeed: 100.59 Mb/s, upspeed: 38.94 Mb/s
time,downspeed,upspeed
12:17:01,100.05,38.28
12:17:21,100.53,37.85
12:17:42,74.87,25.92
We start with a header row then have the information split up. We can think about this like an excel sheet:

import speedtest
import datetime
import csv
import time
s = speedtest.Speedtest()
with open('test.csv', mode='w') as speedcsv:
csv_writer = csv.DictWriter(speedcsv, fieldnames=['time', 'downspeed', 'upspeed'])
csv_writer.writeheader()
while True:
time_now = datetime.datetime.now().strftime("%H:%M:%S")
downspeed = round((round(s.download()) / 1048576), 2)
upspeed = round((round(s.upload()) / 1048576), 2)
csv_writer.writerow({
'time': time_now,
'downspeed': downspeed,
"upspeed": upspeed
})
# 60 seconds sleep
time.sleep(60)
with open
essentiallly creates a csv file with the name test.csv
with the headers name
, downspeed
and upspeed
and writes them into the csv. Then the loop begins and every time a test is performed by speedtest it writes a new row into the csv with the time, download speed and upload speed we specified before. So let’s go and look at that now.
time,downspeed,upspeed
12:51:16,99.29,38.66
12:51:37,100.67,38.79
12:51:57,99.7,38.79
12:52:17,92.89,31.99
12:52:38,99.4,38.96
Cool now we are gathering information and storing it in a csv. You could do all sorts of clever stuff with the filename like name it today’s date using datetime
but for now I will keep it as test.
You could also set up a check to see if a certain amount of time has passed and to stop the application after that but I will leave that up to your own creativity.
Visualising the data
Let’s make another python file to generate the graph of our internet connection. This is where we will use matplotlib.touch make_graph.py
import matplotlib.pyplot as plt
import csv
import matplotlib.ticker as ticker
times = []
download = []
upload = []
The easiest way to generate a graph is by using an array. In order to populate our arrays we will have to iterate through our new csv file
with open('test.csv', 'r') as csvfile:
plots = csv.reader(csvfile, delimiter=',')
next(csvfile)
for row in plots:
times.append(str(row[0]))
download.append(float(row[1]))
upload.append(float(row[2]))
print(times, "\n", download, "\n", upload)
['12:51:16', '12:51:37', '12:51:57', '12:52:17', '12:52:38']
[99.29, 100.67, 99.7, 92.89, 99.4]
[38.66, 38.79, 38.79, 31.99, 38.96]
next(csvfile)
essentially skips the row of headers (that were for our benefit only, not python’s). Now we come on to using matplotlib
which I am by no standards an expert on. Their documentation is extensive.
plt.figure(30, 30)
plt.plot(times, download, label='download', color='r')
plt.plot(times, upload, label='upload', color='b')
plt.xlabel('time')
plt.ylabel('speed in Mb/s')
plt.title("internet speed")
plt.legend()
plt.savefig('test_graph.jpg', bbox_inches='tight')
plt
object to create a figure with the lines labelled ‘download’ and ‘upload’ to be plotted against their respective arrays. So for every item in the download and time array, a mark will be made on the graph against it. Finally the x and y-axis get labels and the graph gets saved.

Potential improvements
This is a short form of a project I recently finished which contains automated uploads to a cloud platform, schedules itself daily does some annotations of the lowest download speed. I initially made it to complain to my internet provider however I know that it will be like showing a graph to a brick wall.
Tweeting about your low internet speed and tagging your ISP
#python
#imageprocessing
October 25, 2020
2 mins read
About Author
Joseph
Developer fluent in Rails, MERN and Python. Now learning Go and looking for a position in Berlin.
Please share your Feedback:
Did you enjoy reading or think it can be improved? Don’t forget to leave your thoughts in the comments section below! If you liked this article, please share it with your friends, and read a few more!