Python internet speed test – How to Check Internet Speed in Python?

Python internet speed test: Today, the internet has become the most convenient way to stay in touch with people and events all around the world. Nowadays, the internet is used for almost every task that needs to be completed. As a result, having a reliable internet connection with adequate speed becomes critical.

We usually use sites like ookla, fast.com, and others to do an internet speed test.
However, did you know that you can use Python to test your internet speed? That’s exciting, isn’t it?

Internet Speed Test:

Install speedtest python: When testing the Internet connection speed, the results are shown as download speed and upload speed.

The speed at which your internet connection takes data from the internet per second is referred to as download speed, while the speed at which your internet connection uploads data to the internet per second is referred to as upload speed. As a result, calculating the download and upload speeds of an Internet connection adds up the Internet speed test.

Checking Internet Speed in Python

NOTE: Run all the commands in the command prompt.

Python has a library called speedtest that is used to test internet speed. It is just a command-line interface for measuring internet bandwidth.

Installation:

pip install speedtest-cli

Output:

Collecting speedtest-cli
Downloading speedtest_cli-2.1.3-py2.py3-none-any.whl (23 kB)
Installing collected packages: speedtest-cli
Successfully installed speedtest-cli-2.1.3

Installing speedtest-cli

To check the version of the library:

speedtest-cli --version

Output:

speedtest-cli 2.1.3
Python 3.9.1 (tags/v3.9.1:1e5d33e, Dec 7 2020, 17:08:21) [MSC v.1927 64 bit (AMD64)]

Example

Approach:

  • Import speedtest module using the import keyword
  • Get the object of the speedtest class using the Speedtest() function and store it in a variable.
  • Print the download speed of the internet using the download() function
  • Print the upload speed of the internet using the upload() function.
  • The Exit of the Program.

Below is the implementation:

# Import speedtest module using the import keyword
import speedtest
# Get the object of the speedtest class using the Speedtest() function and 
# store it in a variable
speed  = speedtest.Speedtest()
# Print the download speed of the internet using the download() function
print("The Download speed of the internet = ", speed.download())
# Print the upload speed of the internet using the upload() function
print("The Upload speed of the internet = ", speed.upload())

Output:

The Download speed of the internet = 1644066450.0077446
The Upload speed of the internet = 737042210.8900017

Checking Internet Speed Over CLI

To obtain the results, we can use the command line interface and run the speedtest-cli command. It returns the results of the speedtest in megabits. As an example, consider the following:

speedtest-cli

Output:

C:\Users\VIKRAM>speedtest-cli
Retrieving speedtest.net configuration...
Testing from ACT Fibernet (183.83.161.82)...
Retrieving speedtest.net server list...
Selecting best server based on ping...
Hosted by City Online Services Limited (Hyderabad) [6.82 km]: 9.937 ms
Testing download speed................................................................................
Download: 19.16 Mbit/s
Testing upload speed......................................................................................................
Upload: 13.37 Mbit/s

Speed In bytes:

You can alternatively get the same results in bytes by using the following command:

speedtest-cli --bytes

Output:

C:\Users\VIKRAM>speedtest-cli --bytes
Retrieving speedtest.net configuration...
Testing from ACT Fibernet (183.83.161.82)...
Retrieving speedtest.net server list...
Selecting best server based on ping...
Hosted by Inet fiber Pvt Ltd (Hyderabad) [6.82 km]: 7.027 ms
Testing download speed................................................................................
Download: 2.68 Mbyte/s
Testing upload speed......................................................................................................
Upload: 3.32 Mbyte/s

To get the Internet speed results in graphical representation, use the below command:

speedtest-cli --share

Output:

C:\Users\VIKRAM>speedtest-cli --share
Retrieving speedtest.net configuration...
Testing from ACT Fibernet (183.83.161.82)...
Retrieving speedtest.net server list...
Selecting best server based on ping...
Hosted by Excitel (Hyderabad) [6.82 km]: 6.951 ms
Testing download speed................................................................................
Download: 19.31 Mbit/s
Testing upload speed......................................................................................................
Upload: 11.10 Mbit/s
Share results: http://www.speedtest.net/result/12889161151.png

Here it returns a link http://www.speedtest.net/result/12889161151.png in the result, which when clicked displays the same output as shown below:

Know more about speedtest library:

Simply run speedtest-cli —help to learn more about all of the options supplied by the speedtest library.
If you’re eager, you can also use the inspect library’s commands to look at the various methods given for speedtest class instances.

speedtest-cli —help