Python is a widely used high-level general-purpose programming language. It is used for web development, machine learning, and even GUI applications. It can also be used to check the speed of the Internet. Python has several libraries for this purpose. One of them is speedtest-cli. Speedtest-cli is a command line interface to check your speed using the speedtest.net service.
Table of Contents
Installation
The module is not pre-installed in Python. To install it you need to type the following command in the terminal:
pip install speedtest-cli
After installing the library, you can check the correctness and version of the package. To do this, use the following command:
& speedtest-cli --version
speedtest-cli 2.1.2
Python 3.8.5 (tags/v3.8.5:580fbb0, Jul 20 2020, 15:57:54) [MSC v.1924 64 bit (AMD64)
Speedtest-cli features
What does Speedtest-CLI do? Speedtest-cli is a module used in the command line interface to check the bandwidth using speedtest.net. To get the speed in megabits type the command: speedtest-cli
. This command will give you the result of the speed in megabits. To get the result in bytes, you need to add one argument to the command.
$ speedtest-cli --bytes
Retrieving speedtest.net configuration...
Testing from ******** (******)...
Retrieving speedtest.net server list...
Selecting best server based on ping...
Hosted by ******** (***) [1.85 km]: 3.433 ms
Testing download speed.........................................................
Download: 22.98 Mbyte/s
Testing upload speed...............................................................
Upload: 18.57 Mbyte/s
You can also use the module to get a graphical version of the test result. There is a parameter for this purpose:
$ speedtest-cli --share
Retrieving speedtest.net configuration...
Testing from ***** (****)...
Retrieving speedtest.net server list...
Selecting the best server based on ping...
Hosted by ***** (***) [1.85 km]: 3.155 ms
Testing download speed.......................................
Download: 164.22 Mbit/s
Testing upload speed............................................................
Upload: 167.82 Mbit/s
Share results: http://www.speedtest.net/result/11111111111.png
The command will return a link that can be clicked in the browser: To display a simpler version of the test results, with only ping, download and upload speed information, use the
--simple
parameter.
$ speedtest-cli --simple
Ping: 3.259 ms
Download: 182.49 Mbit/s
Upload: 172.16 Mbit/s
Using speedtest in Python
Let’s take a look at an example of a Python program for testing Internet speed:
import speedtest
st = speedtest.speedtest()
option = int(input('')
Select the type of test:
1 - Download speed
2 - Download speed
3 - Ping
Your choice: '''))
if option == 1:
print(st.download())
elif option == 2:
print(st.upload())
elif option == 3:
servernames =[]
st.get_servers(servernames)
print(st.results.ping)
else:
print("Please enter a number between 1 and 3!")
The result of running this simple program will be:
Choose the type of check:
1 - Download speed
2 - Download speed
3 - Ping
Your choice: 2
136433948.59785312
Addendum
To dive into the speedtest-cli library use the command --help
to get a list of all available parameters:
speedtest-cli --help # or speedtest-cli -h