Check Internet Speed using Python

Ever wondered just how fast your internet connection is? Check Internet Speed using Python

Maybe you’re gaming online, streaming a movie, or just trying to upload that vacation photo album to the cloud.

But what do those numbers actually mean when you run a speed test?

I decided to take a dive into this with a simple Python script that does the heavy lifting for us. It’s like having a personal speed tester right at your fingertips! 🎉

So, what did I do?

I wrote a small script that measures three key aspects of internet speed:

  • Download speed,
  • Upload speed, and
  • Ping.

What Are These Parameters And What Were My Results?

  • Download speed is how fast data from the internet gets to you.
  • Upload speed is the reverse.
  • Ping measures the time it takes for data to travel from your device to a server and back. The lower the ping, the more responsive your connection feels, which is crucial for things like online gaming or video calls.

I ran the test, and here’s what I got:

  • Download Speed: 13.06 Mbps
  • Upload Speed: 1.77 Mbps
  • Ping: 63.102 ms

The beauty of this little script is that it’s a no-fuss way to check your internet speed.

Plus, you get to see those numbers broken down and understand what they mean.

So next time someone asks, “Is your internet fast?”

you can confidently say, “Let me check!”

and know exactly what those numbers are telling you.

Let’s Check Internet Speed using Python.

Installing and Importing the speedtest module

pip install speedtest-cli
import speedtest as st

Here, you import the speedtest module, which provides tools to test internet speed.

You’re giving it a shorthand name st to make it easier to reference later in the code.

Creating a Speedtest object

def Speed_Test():
    test = st.Speedtest()

You create an instance of the Speedtest class, called test.

This object will handle the actual speed testing

Testing And Rounding Off Download Speed

print("Testing download Speed...")
down_speed = test.download()

print("Rounding off the download Speed...")
down_speed = round(down_speed / 10**6, 2)
print("Download Speed in Mbps: ", down_speed)

down_speed = test.download() runs the download speed test and stores the result in the down_speed variable. The speed is returned in bits per second (bps).

The script prints a message that it’s rounding off the download speed.

It converts the download speed from bps to Mbps (Megabits per second) by dividing it by 10^6.

round(down_speed / 10**6, 2) rounds the speed to two decimal places for readability.

Testing And Rounding Off Upload Speed

print("Testing upload Speed...")
up_speed = test.upload()

print("Rounding off the upload Speed...")
up_speed = round(up_speed / 10**6, 2)
print("Upload Speed in Mbps: ", up_speed)

up_speed = test.upload() runs the upload speed test and stores the result in the up_speed variable, again in bps.

It rounds off the upload speed in the same way as the download speed and then prints it.

Getting The Ping And Calling The Method

    ping = test.results.ping
    print("Ping: ", ping)

Speed_Test()

ping = test.results.ping retrieves the ping result, which measures the latency (the time it takes for a data packet to travel from your device to a server and back).

The ping is measured in milliseconds (ms).

Finally, it prints the ping value.

Full Source Code: Check Internet Speed using Python

import speedtest as st

def Speed_Test():

    test = st.Speedtest()

    print("Testing download Speed...")
    down_speed = test.download()
    print("Rounding off the download Speed...")
    down_speed = round(down_speed / 10**6, 2)
    print("Download Speed in Mbps: ", down_speed)

    print("Testing upload Speed...")
    up_speed = test.upload()
    print("Rounding off the upload Speed...")
    up_speed = round(up_speed / 10**6, 2)
    print("Upload Speed in Mbps: ", up_speed)


    ping = test.results.ping

    print("Ping: ", ping)

Speed_Test()

Leave a Reply