Sunday, 31 May 2026

Build an Internet Connection Checker and Speed Test Tool Using Python

 


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.one

  • Port: 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:

  1. Verify internet access.

  2. If connected, run speed test.

  3. Display results.

  4. 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

Popular Posts

Categories

100 Python Programs for Beginner (119) AI (270) Android (25) AngularJS (1) Api (7) Assembly Language (2) aws (30) Azure (10) BI (10) Books (262) Bootcamp (11) C (78) C# (12) C++ (83) Course (87) Coursera (300) Cybersecurity (31) data (6) Data Analysis (33) Data Analytics (22) data management (15) Data Science (363) Data Strucures (19) Deep Learning (171) Django (16) Downloads (3) edx (21) Engineering (15) Euron (30) Events (7) Excel (19) Finance (10) flask (4) flutter (1) FPL (17) Generative AI (73) Git (10) Google (51) Hadoop (3) HTML Quiz (1) HTML&CSS (48) IBM (42) IoT (3) IS (25) Java (99) Leet Code (4) Machine Learning (309) Meta (24) MICHIGAN (5) microsoft (11) Nvidia (8) Pandas (14) PHP (20) Projects (34) pytho (1) Python (1364) Python Coding Challenge (1148) Python Mathematics (1) Python Mistakes (51) Python Quiz (525) Python Tips (5) Questions (3) R (72) React (7) Scripting (3) security (4) Selenium Webdriver (4) Software (19) SQL (50) Udemy (18) UX Research (1) web application (11) Web development (8) web scraping (3)

Followers

Python Coding for Kids ( Free Demo for Everyone)