Test Your Internet Speed with Python and Source Code

02 May 2023 Balmiki Mandal 0 Python

Internet Speed Test Using Python with Source Code

In this tutorial, we will learn how to create an internet speed test using Python with source code. We will be using the Speedtest-cli library in Python for testing the internet connection speed. Speedtest-cli is a command-line interface for testing internet bandwidth using Speedtest.net. It supports all major operating systems, including Windows, MacOS, and Linux. We will be using a Python script to run the Speedtest-cli library and measure the download and upload speeds of a connected device to the internet. The results of the Speedtest will then be displayed on the user’s screen. Before we start writing the Python script, let’s take a look at the code structure:

import speedtest

# Create an instance of the Speedtest class
st = speedtest.Speedtest()

# Get the download speed
download_speed = st.download()

# Get the upload speed
upload_speed = st.upload()

# Print the download and upload speeds
print("Download speed:", download_speed)
print("Upload speed:", upload_speed)

The first line of code imports the Speedtest library from the speedtest module. We then create an instance of the Speedtest class and assign it to the st variable. After that, we get the download and upload speeds from the Speedtest instance and assign them to the respective variables. Finally, we print out the download and upload speeds on the console. Now, let's run the command-line Speedtest-cli library directly to measure the internet speed of your computer:

$ speedtest-cli --simple

Retrieving speedtest.net configuration...
Testing from [IP Address]...
Retrieving speedtest.net server list...
Selecting best server based on ping...
Hosted by x [Server Name] [Distance] km
Testing download speed................................................................................
Download: [Download Speed] Mbit/s
Testing upload speed......................................................................................................
Upload: [Upload Speed] Mbit/s

Once you have the results of the Speedtest, we can now incorporate it into our Python script. Here is the modified version of our Python script that prints out the download and upload speeds on the console:

import speedtest

# Create an instance of the Speedtest class
st = speedtest.Speedtest()

# Get the download speed
download_speed = st.download()

# Get the upload speed
upload_speed = st.upload()

# Print the download and upload speeds
print("Download speed: %.2f Mbps"%(download_speed/1000000))
print("Upload speed: %.2f Mbps"%(upload_speed/1000000))

The only difference between the modified version of our code and the original one is that we have added formatting to the output so it’s easier to read. That’s it! You now have a simple Speedtest-cli-based Python script that prints out the download and upload speeds on the console. You can use this script to quickly check the internet speed of your device without having to go through the hassle of running the Speedtest command-line tool.

BY: Balmiki Mandal

Related Blogs

Post Comments.

Login to Post a Comment

No comments yet, Be the first to comment.