Test Internet Speed using Python

This article was first published on PyShark , and kindly contributed to python-bloggers. (You can report issue about the content on this page here)
Want to share your content on python-bloggers? click here.

In this article we will discuss how to test internet speed using Python using the speedtest-cli library.

Table of Contents

  • Introduction
  • Installing the required library
  • Description of methods
  • Testing internet speed
  • Conclusion


The internet connections in our homes and offices differ by internet service providers (ISPs), allowable traffic limit, and most importantly speed.

So what do we do when we want to test the speed our connection? Correct, we go on Google and lookup some speedtesting websites and proceed there.

How about trying to test internet speed using Python from your computer in less than 10 lines of code? Let’s take a look!

Installing the required library

In order to continue with examples in this article, you will need to install an additional library speedtest-cli.

If you don’t have them installed, please open “Command Prompt” (on Windows) and install them using the following code:

pip install speedtest-cli

Now that it’s installed, let’s go ahead and import it:

import speedtest

Description of methods

First we will need to create an instance of Speedtest() class and then inspect the methods it has and discuss each of them.

s = speedtest.Speedtest()

The source code for this library doesn’t currently have a lot of detailed information about each method and their usage so we will explore this from scratch.

The next step we are going to take is find out what is actually included in the above class. In other words we want to see what we can use and what information we can retrieve.

Using the inspect library (prebuilt in Python), let’s take a look at what methods the s object has:

import inspect

for method in inspect.getmembers(s, predicate=inspect.ismethod):

We should get the following list as the output:

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

Amazing! Now we found what’s included and now we are going to discuss what operations each of these methods performs.


As you can probably guess from it’s name, this method will test the download speed of your current connection (in bytes). To see it in action:

print('My download speed is:', s.download())

The result I got is: 142857662.477676, which is equivalent to 142.86 Mbps.


This method is similar to the previous one, but tests the upload speed of your current connection (in bytes). To see it in action:

print('My upload speed is:', s.upload())

The result I got is: 235955606.655482, which is equivalent to 235.96 Mbps.


This method allows us to identify the best server that we will be testing the connection from. In general, this tends to find the best testing server that is within your region (city).

In terms of formatting, it will return a dictionary with the details of that server. Let’s take a look:

best_server = s.get_best_server()
for key, value in best_server.items():
    print(key, ' : ', value)

In my case, since I live in Toronto, it found the best server somewhere in downtown of Toronto with the following information:

url : http://tor47spd01.srvr.bell.ca:8080/speedtest/upload.php
lat : 43.6481
lon : -79.4042
name : Toronto, ON
country : Canada
cc : CA
sponsor : Bell Canada
id : 17394
host : tor47spd01.srvr.bell.ca:8080
d : 18.828394194894738
latency : 8.424


This method allows us to a set of servers that are close to our location and we can use these after to do speed tests from different servers/regions.

Similarly to the previous method, we will get back a dictionary, but not instead of one server with details it will be much more.

Here we create store the entire dictionary, but for display purposes only print out the details of the first item in the dictionary:

closest_servers = s.get_closest_servers()
for key, value in closest_servers[1].items():
    print(key, ' : ', value)

And the output shows as:

url : http://speedtest2-tor.teksavvy.com:8080/speedtest/upload.php
lat : 43.6532
lon : -79.3832
name : Toronto, ON
country : Canada
cc : CA
sponsor : Teksavvy Solutions Inc
id : 32632
host : speedtest2-tor.teksavvy.com:8080
d : 18.709590075865655

Notice that the best server is not necessarily the closest one, yet generally they are within the same region/city.


This method allows us to get information on all available servers. It works very similar to the previous method and returns a dictionary with each servers details.

Note: The only concern I came across is they key values used, and not really sure how to interpret them, since they aren’t sequential. But I’m sure that the creator had a logic built in for this.

servers = s.get_servers()
for key, value in servers.items():
    print(key, ' : ', value)

This will return a large dataset, so for purposes of visualizing them, here is an example output for a single server:

url :
lat : 35.5847
lon : -80.8103
name : Mooresville, NC
country : United States
cc : US
sponsor : Continuum
id : 3383
host :
d : 922.322219276907


This method allows us to set the speedtest server by passing a URL to it instead of querying a list of servers.

The server URL can be chosen from any servers that we can retrieve using .get_servers() method, or from this list.

As an example, I chose ‘http://speedtest.oltv.ru/’ and this is how I can integrate it:


And we get a server that was setup for our speed testing:

sponsor : Speedtest Mini
name : speedtest.oltv.ru
d: 0
url : http://speedtest.oltv.ru/speedtest/upload.php
latency : 0
id : 0


This method allows us to find out the current configuration of the speedtest class as well as provides some relevant information about our own connection, including IP, location of servers, internet server provider (ISP), and more.

It’s execution is very simple, and just requires the call of the method without any additional parameters:


And it returns a dictionary of dictionaries with the relevant information.


In this article we discussed how to test internet speed using Python as well as covered the features of the speetest-cli library and showed how some parameters can be adjusted.

Feel free to leave comments below if you have any questions or have suggestions for some edits and check out more of my Python Programming articles.

The post Test Internet Speed using Python appeared first on PyShark.

To leave a comment for the author, please follow the link and comment on their blog: PyShark .

Want to share your content on python-bloggers? click here.