How to Measure Internet Speed In Python Using Speedtest

If you want to measure internet speed then Speedtest is a great website to visit and run the tests, the good news is that there is a Command line interface written in python that means you can quite easily add this functionality to your raspberry pi and create a speed monitor. Lets get started

Step 1: Check for updates

Its good practice to keep your pi up to date, let’s start with checking for the latest updates. Open up a terminal and enter the following commands

sudo apt-get update
sudo apt-get upgrade

Step 2: Install speedtest

In this example we are going to use speedtest-cli which is a command line interface for testing internet bandwidth using Speedtest is one of the best ways to check your internet speed and is useful for checking your upload and download speeds

We need to use pip to install speedtest-cli from the Python Package Index (PyPI). The first thing you should do is verify whether you have pip installed by running this command:

sudo apt-get install python-pip

If you a message that reads “python-pip is already the newest version” that means that we’re good to go as we have the latest version.

pip installed
pip installed

Next, let’s use pip to install speedtest-cli:

sudo pip install speedtest-cli

Now that  speedtest-cli is installed, you can measure your internet speed easily with this command:


This is the default option which means you do not need to specify and command line options, there are many which we have displayed in the Usage section here are my results

speedtest results
speedtest results

Lets try the simple parameters – type the following in the terminal

speedtest-cli --simple

This is what I saw

speedtest simple results
speedtest simple results


This is from – as you can see you can specif

$ speedtest-cli -h
usage: speedtest-cli [-h] [--no-download] [--no-upload] [--single] [--bytes]
                     [--share] [--simple] [--csv]
                     [--csv-delimiter CSV_DELIMITER] [--csv-header] [--json]
                     [--list] [--server SERVER] [--exclude EXCLUDE]
                     [--mini MINI] [--source SOURCE] [--timeout TIMEOUT]
                     [--secure] [--no-pre-allocate] [--version]

Command line interface for testing internet bandwidth using

optional arguments:
  -h, --help            show this help message and exit
  --no-download         Do not perform download test
  --no-upload           Do not perform upload test
  --single              Only use a single connection instead of multiple. This
                        simulates a typical file transfer.
  --bytes               Display values in bytes instead of bits. Does not
                        affect the image generated by --share, nor output from
                        --json or --csv
  --share               Generate and provide a URL to the share
                        results image, not displayed with --csv
  --simple              Suppress verbose output, only show basic information
  --csv                 Suppress verbose output, only show basic information
                        in CSV format. Speeds listed in bit/s and not affected
                        by --bytes
  --csv-delimiter CSV_DELIMITER
                        Single character delimiter to use in CSV output.
                        Default ","
  --csv-header          Print CSV headers
  --json                Suppress verbose output, only show basic information
                        in JSON format. Speeds listed in bit/s and not
                        affected by --bytes
  --list                Display a list of servers sorted by
  --server SERVER       Specify a server ID to test against. Can be supplied
                        multiple times
  --exclude EXCLUDE     Exclude a server from selection. Can be supplied
                        multiple times
  --mini MINI           URL of the Speedtest Mini server
  --source SOURCE       Source IP address to bind to
  --timeout TIMEOUT     HTTP timeout in seconds. Default 10
  --secure              Use HTTPS instead of HTTP when communicating with
               operated servers
  --no-pre-allocate     Do not pre allocate upload data. Pre allocation is
                        enabled by default to improve upload performance. To
                        support systems with insufficient memory, use this
                        option to avoid a MemoryError
  --version             Show the version number and exit

Next Steps

As you can see there are a few options available, you could save the results to a csv file and maybe even graph the results.

Previous articleGetting started with Tkinter on the Raspberry Pi
Next articleADXL362 3-axis accelerometer and Raspberry Pi example