DEV Community

MarkPy
MarkPy

Posted on • Edited on

5 2 1

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.

Let's get started.

Speedtest instalation

To install speedtest via pip, follow this command:

pip install speedtest-cli
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

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 
Enter fullscreen mode Exit fullscreen mode

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))
Enter fullscreen mode Exit fullscreen mode

Output:

3.56 MB
Enter fullscreen mode Exit fullscreen mode

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))
Enter fullscreen mode Exit fullscreen mode

Output:

386382.6586620888
301.51 KB
Enter fullscreen mode Exit fullscreen mode

Speedtest command lines

Speedtest also provides the command lines for testing our internet speed.

Usage:

Help command:

speedtest-cli -h
Enter fullscreen mode Exit fullscreen mode

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.
Enter fullscreen mode Exit fullscreen mode

Test Internet Speed:

speedtest-cli
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

Test Internet Speed with the share link:

speedtest-cli --share
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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/

Python: Test Internet Speed

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (1)

Collapse
 
villerpete78086 profile image
Viller Peter

Speed test net orange is a popular online tool for measuring internet speed. In terms of its performance when used with Orange, the French internet service provider, it appears to provide accurate and reliable results.

Users report that the tool provides quick and easy-to-understand speed measurements, with a clear breakdown of upload and download speeds, as well as ping latency. Additionally, the interface is user-friendly, making it easy to initiate tests and understand the results.

Overall, if you're an Orange customer looking to check your internet speeds, Speedtest.net is a solid choice that is widely trusted by both consumers and industry professionals.

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay