Speedtest your connection in Python

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
No comments yet. Be the first to comment.
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

Today we'll be building our speed testing service in Python. We have Speedtest websites like this to test our ping, upload, and download speed for those who don't know.
For today's article, I was looking to automate this since I check it regularly.
I choose Python as the language, seeing I'm trying that out a bit.
Before we can use this package, we have to install it to become available for us to use.
Use the following command to install it:
pip install speedtest-cli
Now open your python file and start by importing the speed test module.
import speedtest
Then we create a new speed test. In my case, I'm assigning it to the st variable.
st = speedtest.Speedtest()
Note: be aware running the speed test takes a while, so be patient 🙈
Now let's try our download speed and print it out:
print(st.download())
When we run this, we get a long number like this:
55775374.79559286
Now that we know the basics of the speed test, we want to receive three elements:
I'll be showing you how to get this data and format it nicely.
Starting with the ping, for this to work, we need to define a server to ping. In our case let's choose the best one.
st.get_best_server()
After this, we can get the ping to this server by using the following:
print(f"Your ping is: {st.results.ping} ms")
Let's go on to download. We have already seen we can get this by calling the download() function, but it's unformatted.
Below I'll show you how to format it to Mbit/s.
print(f"Your download speed: {round(st.download() / 1000 / 1000, 1)} Mbit/s")
We can make the same approach for the upload but use the upload() function.
print(f"Your upload speed: {round(st.upload() / 1000 / 1000, 1)} Mbit/s")
The full script will look like this:
import speedtest
st = speedtest.Speedtest()
st.get_best_server()
print(f"Your ping is: {st.results.ping} ms")
print(f"Your download speed: {round(st.download() / 1000 / 1000, 1)} Mbit/s")
print(f"Your upload speed: {round(st.upload() / 1000 / 1000, 1)} Mbit/s")
And when we run this, it outputs:
Your ping is: 30.97 ms
Your download speed: 64.4 Mbit/s
Your upload speed: 29.2 Mbit/s
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter