My Projects, Projects

Broadband Speed testing on Raspberry Pi’s

Since I first got cable broadband, I’ve been interested in testing the speed I am getting. Over the years this has gotten easier with tools like speedtest.net. The only issue with Speedtest.net was that it was designed for use in a browser.

However – the community came the rescue and a github repository popped up with “Speedtest-cli” an unlicensed open source Command Line Interface (CLI) that used scripts to allow you to run the speedtest from the command line on Linux machines.

This tool works fantastically and is good to go. With a Python wrapper you can even code a speed test into your applications. Indeed I did with my early Netapp tools. However since I got my first Raspberry Pi 4 board, I found this version lacking and it would not give good results. I am on Virgin Media’s 350Mb/s package and my wired machines show I get 350Mb/s. But Speedtest-cli was maxing out at 200Mb/s.

Whilst looking for a solution, I found that Ookla (the company behind Speedtest.net) had brought out an official CLI version of their tools.

You can find information about this tool on their Speedtest.net website: https://www.speedtest.net/apps/cli

Installing their tool is a little more complex than installing the unofficial version as they have opted to run their own package manager. However, it is not too complicated. Just copy the lines below one at a time and run them on the command line on your Ubuntu Based Raspberry Pi.

sudo apt-get install gnupg1 apt-transport-https dirmngr

export INSTALL_KEY=379CE192D401AB61

export DEB_DISTRO=$(lsb_release -sc)

sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys $INSTALL_KEYecho "deb https://ookla.bintray.com/debian ${DEB_DISTRO} main" | sudo tee  /etc/apt/sources.list.d/speedtest.list

sudo apt-get update

# Other non-official binaries will conflict with Speedtest CLI

# Example how to remove using apt-get
# sudo apt-get remove speedtest-cli

sudo apt-get install speedtest

Once the last line is run, the next thing to do is to run it once to agree to Ookla’s terms and conditions of usage.

speedtest
Running for the first time will ask you to accept the terms and conditions.

Just type in “YES” and press return/enter and the first speed test will run.

An example of the Ookla Speedtest CLI running for the first time

When you accept the agreement, the tool creates a json file under your home directory in “~/.config/ookla/” called “speedtest-cli.json”.

If you remove this file or directory – you will need to accept the terms and conditions and license again.

Running in Python 3

One thing missing from the official CLI is a python wrapper, so if you want to run a speedtest in your Python code, you will need to write you own.

I have written a very basic version for you to play with:

import subprocess
import json


stdoutdata = subprocess.getoutput("speedtest -f json")
#print(stdoutdata.split()[0])
results = json.loads(stdoutdata)
for key in results:
   download = results["download"]["bandwidth"] 
   upload = results["upload"]["bandwidth"]
   isp = results["isp"]

down = str(round(download / 125000, 2))
up = str(round(upload / 125000, 2))

print("Download: "+ down + "Mbps")
print("Upload: " + up + "Mbps")
print("Internet Provider: " + isp)

I hope you find all this helpful!

Find me on Twitter and Facebook if you want to discuss it!

en flag
fr flag
de flag
es flag
Play with Alexa
Voiced by Amazon Polly

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.