Last modified: Jan 10, 2023 By Alexander Williams
Python: Test Internet Speed (Download - Upload)
Probably, you have used the speedtest.net website before, but did you know that speedtest has a python library.
In this tutorial, We will learn how to use the speedtest library to test our speed connection. We'll also learn how to use speedtest command lines.
Let's get started.
Speedtest instalation
To install speedtest via pip, follow this command:
pip install speedtest-cli
Test internet speed using code
After installing the speedtest package, now let's see how to use it with the code.
In the following code, I'll test my internet speed download.
import speedtest
# Speed test
st = speedtest.Speedtest()
# Download Speed
ds = st.download()
print(ds)
let me explain.
First, we import the speedtest package. Then, called Speedtest() class. Next, test my download speed using the download() method. Finally, print the result.
Output:
3422459.073187817
As you can see, the internet speed is in Bytes. To make it readable, we'll use the following function.
def humansize(nbytes):
suffixes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB']
i = 0
while nbytes >= 1024 and i < len(suffixes)-1:
nbytes /= 1024.
i += 1
f = ('%.2f' % nbytes).rstrip('0').rstrip('.')
return '%s %s' % (f, suffixes[i])
#Readable
print(humansize(ds))
Output:
3.56 MB
Now, let's test our upload speed using the upload() method.
import speedtest
# Speed test
st = speedtest.Speedtest()
# Upload speed
us = st.upload()
print(us)
#Readable
print(humansize(us))
Output:
386382.6586620888 301.51 KB
Speedtest command lines
Speedtest also provides the command lines for testing our internet speed.
Usage:
Help command:
speedtest-cli -h
Output:
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 speedtest.net.
Speed test command:
speedtest-cli
Output:
Retrieving speedtest.net configuration... Testing from xxx Telecom (196.89.30.99)... Retrieving speedtest.net server list... Selecting best server based on ping... Hosted by xxx Telecom (xxx) [394.57 km]: 29.1 ms Testing download speed................................................................................ Download: 3.30 Mbit/s Testing upload speed...................................................................................................... Upload: 0.33 Mbit/s
This command above tests ping, download speed, and upload speed.
Speed test command with the share link:
speedtest-cli --share
Output:
Retrieving speedtest.net configuration... Testing from xxx Telecom (196.89.30.99)... Retrieving speedtest.net server list... Selecting best server based on ping... Hosted by xxx Telecom (xxx) [394.57 km]: 28.131 ms Testing download speed................................................................................ Download: 3.74 Mbit/s Testing upload speed...................................................................................................... Upload: 0.39 Mbit/s Share results: http://www.speedtest.net/result/12339819892.png
As you can see, we've got a URL of the results. Let's use it in the browser.
Result: