Build an Internet Connection Checker and Speed Test Tool in Python
Check Your Internet Status and Measure Network Speed with Python
Have you ever wondered if your internet is actually connected before running an online application? Or wanted to measure your download speed, upload speed, and ping directly from Python?
In this tutorial, you'll learn how to create two useful networking tools:
✅ Internet Connection Checker
✅ Internet Speed Tester
These tools are practical for developers, system administrators, students, and anyone interested in networking with Python.
Project 1: Internet Connection Checker
Before performing any online task, it's a good idea to verify that an internet connection is available.
Python Code
import socket
def check_connection(host="one.one.one.one", port=53, timeout=3):
try:
socket.create_connection((host, port), timeout=timeout)
return True
except OSError:
pass
return False
if check_connection():
print("Internet connection is active.")
else:
print("No internet connection detected.")
How It Works
Import Socket Module
import socket
The socket module provides access to low-level networking interfaces.
Create a Connection
socket.create_connection((host, port), timeout=timeout)
The program attempts to connect to Cloudflare's DNS server.
Host:
one.one.one.onePort:
53(DNS service)
If the connection succeeds, the internet is considered active.
Handle Errors
except OSError:
If the connection fails, Python catches the error and returns False.
Example Output
Internet connection is active.
or
No internet connection detected.
Project 2: Internet Speed Test
Now let's measure actual network performance.
This tool reports:
Download Speed
Upload Speed
Ping (Latency)
Install Required Library
pip install speedtest-cli
Python Code
import speedtest
st = speedtest.Speedtest()
download = st.download() / 1_000_000 # to Mbps
upload = st.upload() / 1_000_000 # to Mbps
ping = st.results.ping # ms
clcoding = {
"download_mbps": round(download, 2),
"upload_mbps": round(upload, 2),
"ping_ms": round(ping, 2)
}
print(clcoding)
Understanding the Code
Import the Library
import speedtest
The Speedtest library connects to nearby speed-testing servers.
Create a Speedtest Object
st = speedtest.Speedtest()
This initializes the speed testing engine.
Download Speed Test
download = st.download() / 1_000_000
The library returns bits per second.
We divide by 1,000,000 to convert the result into Mbps.
Upload Speed Test
upload = st.upload() / 1_000_000
Measures how quickly data can be sent to the internet.
Ping Test
ping = st.results.ping
Ping represents network latency.
Lower values indicate faster response times.
Example Output
Connecting to the nearest server, please wait...
Testing download speed...
Testing upload speed...
--- Speed Test Results ---
Download: 95.42 Mbps
Upload: 22.67 Mbps
Ping: 12.15 ms
Why These Projects Are Useful
These tools can be integrated into:
Network monitoring systems
System health dashboards
IT support utilities
Automated troubleshooting scripts
Raspberry Pi monitoring projects
Home network diagnostics
Combine Both Programs
A practical improvement is to check for internet connectivity before running the speed test.
Workflow:
Verify internet access.
If connected, run speed test.
Display results.
Handle network errors gracefully.
This creates a more reliable networking utility.
Key Python Concepts Learned
Through these projects, you practiced:
Functions
Exception Handling
Networking with Sockets
External Python Libraries
Speed Measurement
Conditional Statements
Real-World Automation
Conclusion
Python makes it incredibly easy to work with networking tasks. Using just a few lines of code, you can verify internet connectivity and measure network performance in real time.
Whether you're building a network monitoring tool, troubleshooting connectivity issues, or learning Python networking, these projects provide practical hands-on experience with real-world applications.
Start experimenting today and transform Python into your personal network diagnostic toolkit!




0 Comments:
Post a Comment