As a developer, I often get requests from clients to create small utilities that solve very specific problems. One of my recent projects involved building a lightweight, script-based Internet Speed Test tool using Python.
The client needed a way to quickly assess internet performance, track the results over time, and log the data for analysis. They wanted no fancy GUI just something clean, fast, and functional.
Let me walk you through how I built this tool using the speedtest-cli
Python module, added logging features, and made the script user-friendly.
Code Explanation
To get started, I used the speedtest-cli
library, a wrapper for Speedtest.net that allows programmatic access to speed test data.
Here’s the base version I wrote first:
# Install the speedtest-cli package using pip
# pip install speedtest-cli
import speedtest
# Create a Speedtest object
st = speedtest.Speedtest()
# Get download speed in Mbps
download = st.download() / 1024 / 1024
# Get upload speed in Mbps
upload = st.upload() / 1024 / 1024
# Get ping in milliseconds
ping = st.results.ping
# Display the results
print(f"Download Speed: {download:.2f} Mbps")
print(f"Upload Speed: {upload:.2f} Mbps")
print(f"Ping: {ping:.2f} ms")
Define Each Line Does:
pip install speedtest-cli
: Installs the required package.import speedtest
: Loads the module.st = speedtest.Speedtest()
: Creates the object.download/upload
: Runs the speed test and converts the result to Mbps.ping
: Measures latency in milliseconds.print(...)
: Neatly displays the results to the user.
While this worked great for a quick speed check using python, the client requested some upgrades to make it more robust.
Extended Version
To better meet the project’s needs, I extended the script with:
- Best server selection for more accurate results
- Timestamp logging for context
- CSV file output for record-keeping
- Optional looping for repeated testing
Here’s the enhanced version I delivered:
speedtest
import csv
from datetime import datetime
def test_internet_speed():
st = speedtest.Speedtest()
# Get best server
st.get_best_server()
# Run tests
download = st.download() / 1024 / 1024 # Mbps
upload = st.upload() / 1024 / 1024 # Mbps
ping = st.results.ping # ms
# Time of test
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
# Display results
print("\n📡 Internet Speed Test Results")
print(f"Time: {timestamp}")
print(f"Download Speed: {download:.2f} Mbps")
print(f"Upload Speed: {upload:.2f} Mbps")
print(f"Ping: {ping:.2f} ms")
# Save results to CSV
with open("internet_speed_results.csv", mode="a", newline="") as file:
writer = csv.writer(file)
writer.writerow([timestamp, f"{download:.2f}", f"{upload:.2f}", f"{ping:.2f}"])
if __name__ == "__main__":
repeat = input("Do you want to run the test multiple times? (y/n): ").lower()
if repeat == 'y':
count = int(input("How many times? "))
for i in range(count):
print(f"\nRunning test {i+1}/{count}...")
test_internet_speed()
else:
test_internet_speed()
New Functionalities Include
Feature | Purpose |
---|---|
get_best_server() | Selects the most optimal nearby server for better accuracy |
CSV Output | Saves results to internet_speed_results.csv |
Timestamp | Logs when each test was performed |
Loop Option | Allows multiple tests in a session |
Sample Output CSV
Here’s what the CSV file looks like after running the test a couple of times:
-06-20 10:15:45, 19.10, 27.51, 8.32
2025-06-20 10:20:13, 18.95, 26.88, 9.10
This makes it super easy for the client to track changes in their internet connection over time or generate weekly performance reports.
Final Thoughts
Working on this project reminded me how Python shines for quick utility tools. The combination of readable syntax and a vast ecosystem of libraries makes Python an excellent choice for building productivity scripts like this.