How to Test Internet Speed Using Python?

How to Test Internet Speed Using Python

In this tutorial, I will show you that how to test internet speed with the python code and using the speedtest-cli package.

There are many internet service providers and they allow the different internet speed to our customers. Each one has a limit of downloadable and upload speed connection. And here you want to check that what is the speed limit that your service provider giving you.

When you want to check your internet speed, you mostly probally search on google for internet speed testing websites. You will get many websites like fast.com and speedtest.net and you can check on these websites.

BUT what if we say that you can also test the internet speed using python?

Yes! you can.

You just have to install the speedtest-cli python library of speedtest.net. It has a method to check your internet ping, download and upload speed and many other things.

So, let’s get started!

Test Internet Speed Using Python Code

Python is a very popular and high-level programming language and it is used for many purposes like website development, AI, Robotics Machine learning, and many more. It has many in-built packages and also has the ability to extend the functionality by installing the external package.

So, there are also many packages for speed test but today in this tutorial we will cover speedtest-cli package and how to use to check internet speed.

Install the speedtest library

To check the internet speed with python, firstly you need to install speedtest-cli library using the following command.

pip install speedtest-cli

The above command will install the library and it’s ready for use. Now you can use this library’s methods to check internet speed.

After installing, you need to create an instance of the Speedtest() class.

Now you can use method of this instance. If you don’t what methods are exist in this class then you can check here for more details.

You can also inspect what methods are exist using the inspect library.

Inspect library example:

import inspect

for method in inspect.getmembers(st, predicate=inspect.ismethod):
print(method[0])

It will print the following list:

  • get_config
  • get_servers
  • get_best_server
  • get_closest_servers
  • set_mini_server
  • download
  • upload

You can use it as per your requirements.

Check the Download Speed

From the above list, we will use the download() function to get the internet downloading speed. It will show the download speed of your current internet connection. See the following code.

The output of the above code will be in bytes like 113783892.934546 (113.37MB estimation).

Check the Upload Speed

To check the upload speed of your current internet connection, you have to use the upload() function. It will print the upload speed in bytes. See the following example.

You will get the output result in bytes like the example above one (93283792.378167 (93.28MB estimation))

Description of Other Methods

As you saw in the list of above existing methods, and there are more functions which you can use if you need them. Each function shows the different properties of the result and you can extract the key and value from the loop of the function.

Let’s see the brief of each function.

get_servers(): This method returns the information about all available servers. It will give you result in the dictionary and have a key pairs value set.

get_best_server(): This method is used to find the best testing server in your region and it will identify that server to test your connection. It will also return the result in a dictionary set.

get_closest_servers(): This method will return the list of servers that are close to your region (by location). This method is used to test the connection from the different severs.

set_mini_server(): With this method, you can set the server by URL. The URL of any server you can get from the get_servers() function.

get_config(): This method will return our current configuration of speed test server. It will return IP address, location of server, your ISP (internet service provider), etc.

Test Internet Speed with Command-line Interface

Yes! you can also you this python library with a command-line interface to test your internet speed.

After installing this library, you can use the below commands to test internet speed.

Before going to check speed, make sure you have installed it correctly. You can check this with the below command.

speedtest-cli --version

This command will return the current version of speedtest-cli library. If you get this it means it installed correctly and you can proceed with the next command to check internet speed.

To check the speed in megabytes use the following command.

speedtest-cli

It will return the speed in megabytes. If you want speed in bytes then use the following command.

speedtest-cli --bytes

This is cool. You will get your internet speed on your command-line interface. There is another more command, if you want to see your speed details visually then use the below command.

speedtest-cli --share

This command will give you a link and you can open it and see your current internet speed visually.

That’s it!!!

Hope you understand the tutorial to check internet speed. if you have any questions please ask me in the comment section, I’ll respond to you as soon as possible.

More Python Tutorials:

How to Create Nested Functions in Python?
Multiple Constructors For Class in Python

You May Also Like

About the Author: Aman Mehra

Hey! I'm Aman Mehra and I'm a full-stack developer and have 5+ years of experience. I love coding and help to people with this blog.

Leave a Reply

Your email address will not be published. Required fields are marked *