Check internet speed in Python using speedtest-cli
The internet today has become the easiest medium to connect with people and happenings across the world. Every second work you want to get done involves the internet these days. Thus, having a stable internet connection with good speed becomes very important.
Usually, whenever we want to perform an internet speed test, we make use of sites like ookla, fast.com etc.
However, did you know that you can test your internet speed using Python? That’s interesting, right?
In this tutorial, you will learn how to check your internet speed using Python.
What exactly is the internet speed test?
An internet speed test is usually run to measure the travel speed between your device and the server you want to connect to, over the internet connection you are using. It usually displays the upload and download speeds as the result.
Download speed refers to the amount of data per second that it takes to download data from a server.
On the other hand, upload speed refers to the amount of data per second that it takes to send data to a server.
Checking the internet speed in Python
Python provides a library called speedtest which is useful for testing the internet speed. It is basically a command-line interface for checking the internet bandwidth.
First of all, install the above library by running the following pip command:

Once this is done, you can verify your installation by checking for the version of speedtest installed.

Once, this is done, you can simply proceed to run the test.
We first initialize an instance of the speedtest class (speed) and then use it to invoke the download() and upload() methods to get the respective speeds as shown.
Testing the internet speed over the CLI
We can run the speedtest-cli command on the command line interface to get the results. It returns the speedtest results in megabits. The same is shown below:

You can also avail the same results in bytes by specifying it in the command as follows:

If you want to get the results in graphical format, you can use the command:
It returns a link in the result, on visiting which you can see the same output represented as follows:
Build an Internet Speed Tester Using Python
As internet users, we usually rely on fast.com and speedtest.net for all of our internet performance and network speed tests. Although this is not bad in terms of utilities, it would be great if we could integrate these tools in our own applications to run all these tests inside the core of our project, without the need of going back and forth in between our app and these tools, wouldn’t be great if we could do so?
Well, we certainly can and we will focus on speedtest.net to give you guys an example on how to proceed with this in this article, are you ready to start this journey, buddy?
Requirements:
We will need three modules, or packages, or libraries or whatever you want to call it in order for us to run this project. We can install them using pip or downloading the files. Most of these packages have their own descriptive section in the the Python Package Index(PyPI)repository. Let’s add a summary of some of these:
| Package Name | Package Website and Details |
|---|---|
| SPEEDTEST | https://pypi.org/project/speedtest/ |
| Description | Speed test module for Python |
| Version | speedtest==0.0.1 |
| Command | pip install speedtest |
| Requires | Python >=3.7 |
| Package Name | Package Website and Details |
|---|---|
| COLORAMA | https://pypi.org/project/colorama/ |
| Description | Cross-platform colored terminal text. |
| Version | colorama==0.4.4 |
| PIP Command | pip install colorama |
| Requires | Python >=2.7, !=3.0.*, |
| Package Name | Package Website and Details |
|---|---|
| TQDM | https://pypi.org/project/tqdm/ |
| Description | Fast, Extensible Progress Meter |
| Version | tqdm==4.62.3 |
| PIP Command | pip install speedtest |
| Requires | Python !=3.0., !=3.1. to !=3.3.*, >=2.7 |
Now that we have all these modules installed, let’s create a speed_test_pjt.py file and start our first lines of code by adding the necessary imports in this file as follows:
Exit fullscreen mode
Next, we need to initialize our colorama module, passing the autoreset parameter as True, that way, the console colors will automatically reset to default after the last colorama command has been invoked. I am pretty sure you will love this amazing package, buddy!
Exit fullscreen mode
Once we have added this, we proceed to create an instance of the Speed Test class using the SpeedTest() function, get the most optimal server available for our test using get_best_server() in our instance, calling the download() and upload() functions to get downloading and uploading speed information, and save all this data in a dictionary called res_dict that will contain all the returning results of these functions.
We will use tqdm() method of our installed progress bar to display in the screen a fake progress of the speed test events so that way something is being shown in the screen while these events are taking place. Let’s see the code below, young coders:
Exit fullscreen mode
Next, let’s retrieve the information from res_dict and assign to variables with a specific format. This will come handy is terms of displaying more readable data:
Exit fullscreen mode
Last but not least, let’s give that nice looking table effect to our results combining the power of colorama, along with some lines, used as dividers, to make it look smooth, shall we?
Exit fullscreen mode
Ready, tigers!! We can now say that our code has been completed: Congrats! You guys did a great job!
Final Code for this Project
Exit fullscreen mode
❤️ Like if you enjoyed this article, that motivates to write more!
Please leave your comment, your opinion very important.
If you truly consider this article helped you wholly!
Python Internet Speed Test
I have been working remotely for a while now, and every time I go to a new place I perform an internet speed test. I need to know if the internet speed is high enough for me to have video calls. Many different websites already exist such as fast.com, speedtest.net, and speedcheck.org. A few weeks ago I started to think, how do these websites actually measure your internet speed? And more importantly, what do these different metrics actually mean. In this article, I will explain to you how a basic internet speed test works and how you can create an internet speed test using Python.
Internet Speed Test
Every time you do a speed test on one of these aforementioned websites, you are loading a file from a server. Ideally, this server is located close to you, as you can imagine that the further a server is physically located from your device, the longer it takes to load a file. This is essential as your internet speed is based on the total time it takes you to download that file. These websites know the exact size of that file and can calculate your speed with the following formula: S = A : T

This formula is super simple so I won’t take any time explaining it. The most important thing you need to know about this formula is that the result will be in Mbps. All of these websites that I have mentioned before will show your internet speed in Mbps. I always thought this meant Megabytes per second like if you would have a file of 1 Megabyte, it would take me 1 second to download it. However, this is not the case! Mbps means Megabits per second. 1 Byte is actually 8 bits (read this website for more info). This means that it will take you 8 seconds to download a file of 1 Megabyte with a speed of 1 Mbps (1 Megabit per second).

As you will learn later in this article, these websites will actually divide a file into multiple chunks. Then it will take the average speed for all these different chunks to calculate your internet speed. This is why it takes a few seconds do an internet speed test, while you might be only downloading 1 MB (megabyte) of data.
Metrics
So amazing, you have just performed an internet speed test and you have gotten the following metrics:
- Download Speed (Mbps)
- Upload Speed (Mbps)
- Ping (ms)
Download Speed and Upload Speed
I have already explained Download and Upload Speed in the previous paragraph. Divide whatever number you have gotten with 8 and you can calculate how long it will take to download a file if you know its size.
Ping (ms)
I haven’t discussed Ping yet. But this is actually one of the most important metric for me, as I do a lot of video calls. Ping will tell you something about the latency of your connection, in other words how long it takes for you to send a request to a server plus the time it takes for the server’s response to come back. Reason that this is so important for video calling is that you are streaming data from a server, but you are not able to buffer anything. You might know the word buffering from watching a video. Whenever you are watching a video you are downloading a file while you are loading it. If your internet speed is fast enough, but not stable enough, you shouldn’t experience any problems.
However, whenever your ping is high this means your internet speed is unstable. When video calling you can not compensate for this with a fast internet speed, as there is nothing to buffer yet. Everything happens in real-time and you need to load the frames in real-time. So if your ping is high, you will experience issues with video calling.
How to do an internet speed test with Python without modules
So before I show you the easiest way of doing an internet speed test with Python, I will show you how you will do this without importing a module. I think this will improve your understanding of how an internet speed test works. As you can see in the code below, we are loading a file with a certain size (1MB). We are then splitting this file into 1024 bits (128 bytes). As discussed previously, we take the average time it took per chunk of file to determine the total speed test.
How to measure Ping with Python without a module
To measure the ping (latency) of your internet speed, we can send a really small file to a server and wait for its response. We will do this multiple times as well, and get the average time to determine our ping.
How to do an internet speed test with Python (Speedtest-cli)
The easiest way of doing an internet speed test with Python is using the module speedtest-cli
In the code below you can see the internet speed with Python. The function test() contains the actual speed test and is normally sufficient. The extra code in the main() function is just to get a nice output for all your speedtest. Note that this speed test is more accurate as you are first selecting the best server and then loading the file from that server. Like I said, the further the server, the longer it will take for you to download the file.
How to measure ping using Python (Pingpython)
Of course there is an easier way of measuring the ping of your internet speed. This involves using a module called pingpython
The code to measure the ping using Python is really straightforward. Again, we are taking multiple measurements and use the average of those to determine our ping. In this example, we are pinging two different servers to get a more accurate result.
I hope you enjoyed learning about how to do a speed test in Python. If you are completely new, check out my article about installing Python with Anaconda. If you have any questions, do not hesitate to contact me.
Internet Speed Measurement Using Speedtest-cli
SpeedTest.net is an excellent website that lets you measure internet speed from your Raspberry Pi. It’s useful for checking the performance either for fault finding purposes or to see if you are getting the service promised by your ISP.
Matt Martz has created a Python project called speedtest-cli which allows you to do a basic upload/download measurement using SpeedNet’s infrastructure. It works fine on the Pi and is really easy to try out on the command line.
Installation
Downloading the utility is easy if you clone the projects git repository :
Running SpeedTest with Python
To run SpeedTest on the command line you need to navigate to the directory :
and then run the Python script using Python 3 :
The script will run and you should see something like this :

Share Internet Speed Results
If you add –share to the command like this :
it will create a link to an online image showing your results which you can share with your friends/family/enemies.

Here’s my result using WiFi on a Pi 3 :

and with Ethernet :

The improved Ethernet bandwidth of the Pi 3 B+ gives a massive difference over previous models.
Installing using Apt
It is possible to install SpeedTest using Apt. This makes it slightly easier to run from the terminal but it may not be the latest version.