DEV Community

loading...
Cover image for Speedtest your connection in Python

Speedtest your connection in Python

Chris Bongers
Looking to get into development? As a full-stack developer I guide you on this journey and give you bite sized tips every single day 👊
Originally published at daily-dev-tips.com ・2 min read

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.

Installing the speedtest-cli in Python

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
Enter fullscreen mode Exit fullscreen mode

Now open your python file and start by importing the speed test module.

import speedtest
Enter fullscreen mode Exit fullscreen mode

Then we create a new speed test. In my case, I'm assigning it to the st variable.

st = speedtest.Speedtest()
Enter fullscreen mode Exit fullscreen mode

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())
Enter fullscreen mode Exit fullscreen mode

When we run this, we get a long number like this:

55775374.79559286
Enter fullscreen mode Exit fullscreen mode

Making a full Python speed test script

Now that we know the basics of the speed test, we want to receive three elements:

  • ping
  • download
  • upload

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()
Enter fullscreen mode Exit fullscreen mode

After this, we can get the ping to this server by using the following:

print(f"Your ping is: {st.results.ping} ms")
Enter fullscreen mode Exit fullscreen mode

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")
Enter fullscreen mode Exit fullscreen mode

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")
Enter fullscreen mode Exit fullscreen mode

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")
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Thank you for reading, and let's connect!

Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter

Discussion (2)

Collapse
mccurcio profile image
Matt Curcio

Great idea, Thanks

Collapse
dailydevtips1 profile image
Chris Bongers Author

Thanks, glad you like it ⚡️