How to Test Internet Speed in Python

Probably, you have used the speedtest website before, but did you know that speedtest has a python library.

In this tutorial, We will learn to use the speedtest library to test your internet Speed. We’ll also learn how to use speedtest command lines.


This content originally appeared on DEV Community and was authored by saidpy

Probably, you have used the speedtest website before, but did you know that speedtest has a python library.

In this tutorial, We will learn to use the speedtest library to test your internet Speed. 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 (script)

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 download speed.

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 the 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.

Test Internet Speed:

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: 8.30 Mbit/s
    Testing upload speed......................................................................................................
    Upload: 10.33 Mbit/s

This command above tests ping, download, and upload speed.

Test Internet Speed 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 open it in the browser.

Result:
Image description

I hope this is easy to understand. See you later.

References

https://pypi.org/project/speedtest-cli/
speedtest example (PyOnlyCode)
Speedtest-cli Example (PyOnlyCode)


This content originally appeared on DEV Community and was authored by saidpy


Print Share Comment Cite Upload Translate Updates
APA

saidpy | Sciencx (2021-12-06T20:56:59+00:00) How to Test Internet Speed in Python. Retrieved from https://www.scien.cx/2021/12/06/how-to-test-internet-speed-in-python/

MLA
" » How to Test Internet Speed in Python." saidpy | Sciencx - Monday December 6, 2021, https://www.scien.cx/2021/12/06/how-to-test-internet-speed-in-python/
HARVARD
saidpy | Sciencx Monday December 6, 2021 » How to Test Internet Speed in Python., viewed ,<https://www.scien.cx/2021/12/06/how-to-test-internet-speed-in-python/>
VANCOUVER
saidpy | Sciencx - » How to Test Internet Speed in Python. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/12/06/how-to-test-internet-speed-in-python/
CHICAGO
" » How to Test Internet Speed in Python." saidpy | Sciencx - Accessed . https://www.scien.cx/2021/12/06/how-to-test-internet-speed-in-python/
IEEE
" » How to Test Internet Speed in Python." saidpy | Sciencx [Online]. Available: https://www.scien.cx/2021/12/06/how-to-test-internet-speed-in-python/. [Accessed: ]
rf:citation
» How to Test Internet Speed in Python | saidpy | Sciencx | https://www.scien.cx/2021/12/06/how-to-test-internet-speed-in-python/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.