If you’re anything like me, you’re probably tired of switching between browsers, ads, and confusing interfaces just to check your internet speed. So I decided to fix that with Python.
Let me walk you through how I built a simple script to check my download speed, upload speed, and ping using just a few lines of code. And yes, it works in seconds.
Installation
Before we dive into the code, we need to install a small but powerful library:
install speedtest-cli
This command installs the speedtest
Python module, which communicates with Speedtest.net to test your internet bandwidth. It’s super easy and lightweight perfect for quick automation tasks.
Import the Module
Now, let’s import the library we just installed.
import speedtest
This makes all the core functions of Speedtest available in your Python script.
Create a Speedtest Object
Next up, we initialize the Speedtest object:
= speedtest.Speedtest()
This object gives us access to the full toolkit methods to get the fastest server, run speed tests, and fetch results.
Measure Speeds
Let’s now get the actual speed results.
= st.download() / 1024 / 1024
upload = st.upload() / 1024 / 1024
- The
download()
andupload()
methods return speeds in bits per second. - To convert those into megabits per second (Mbps) (which is what most of us are familiar with), we divide by
1024 * 1024
.
Measure Ping
We also want to check latency, or ping, which is super important for real-time tasks like gaming or video calls.
= st.results.ping
This returns the ping time in milliseconds (ms). Lower values = better performance!
Print the Results
Let’s display everything in a clean, human-readable format.
print(f"Download Speed: {download:.2f} Mbps")
print(f"Upload Speed: {upload:.2f} Mbps")
print(f"Ping: {ping:.2f} ms")
This gives us a neat output like:
Download Speed: 19.10 Mbps
Upload Speed: 27.51 Mbps
Ping: 8.32 ms
Pretty cool, right?
Enhance Version with Logging & Menu
I wanted to push this further, so I built a more advanced version that:
- Finds the best server automatically
- Logs results with timestamp
- Lets you choose options from a menu
Here’s the upgraded code:
speedtest
import datetime
def test_speed():
print("Testing... Please wait.\n")
st = speedtest.Speedtest()
st.get_best_server()
download = st.download() / 1024 / 1024
upload = st.upload() / 1024 / 1024
ping = st.results.ping
print(f"Download Speed: {download:.2f} Mbps")
print(f"Upload Speed: {upload:.2f} Mbps")
print(f"Ping: {ping:.2f} ms")
# Save result to file
now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
with open("internet_speed_log.txt", "a") as log:
log.write(f"{now} - Download: {download:.2f} Mbps, Upload: {upload:.2f} Mbps, Ping: {ping:.2f} ms\n")
print("\nResults saved to internet_speed_log.txt")
def list_servers_and_choose():
st = speedtest.Speedtest()
servers = st.get_servers()
print(f"\n{len(servers)} servers detected. Using best one automatically.\n")
def show_menu():
while True:
print("\n--- Internet Speed Test Menu ---")
print("1. Run Speed Test")
print("2. List Available Servers")
print("3. Exit")
choice = input("Select an option: ")
if choice == '1':
test_speed()
elif choice == '2':
list_servers_and_choose()
elif choice == '3':
break
else:
print("Invalid option. Please try again.")
if __name__ == "__main__":
show_menu()
Bonus Features in This Script
- Logs every test with a timestamp (
internet_speed_log.txt
) - Auto selects best server
- Simple menu interface for repeated use
Final Thoughts
Honestly, I didn’t expect measuring internet speed with Python would be this fun. This tiny project made me appreciate how powerful and fast Python can be for even the simplest daily tasks.
Whether you’re a developer testing latency before a big Zoom call, or just curious if your ISP is giving you what you’re paying for—this tool has got your back.
Feel free to modify it, add a GUI with Tkinter
, or even turn it into a scheduled job using cron
or Task Scheduler
.