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:
pip install speedtest-cli
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.
import speedtest speed = speedtest.Speedtest() print("Download Speed: ", speed.download()) print("Upload Speed: ", speed.upload())
Download Speed: 40431055.713519335 Upload Speed: 26263546.373754457
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:
- To know more about all the options that are provided by the speedtest library, you can simply run;
- If curious, you can also use the inspect library’s commands to get a look at different methods provided for the instances of speedtest class.
Also read: Test Internet Speed using Python