Skip to content

Network Connection Tester

In plain English

A lightweight network tool built only with Python's standard library to time website address lookups and check whether server ports respond cleanly.

A stdlib-only Python 3.12 diagnostic library decoupling host validation, DNS lookup timing, and parallel TCP port probing.

1. Why I built it

Built as a portable diagnostic module runnable on any Python 3.12 host to distinguish network failure states without external packages.

Problem being solvedSequential port scans compound timeouts linearly, and bad port inputs or unguarded socket exceptions waste lookup time.

2. How it works

Implemented `validate_host()`, `resolve_dns()`, `check_tcp_connectivity()`, and parallel `build_diagnostic_report()` in `src/net_diag/diagnostics.py`.

ICMP `ping` cannot isolate application ports and stalls behind firewall drops, whereas bare Python socket exceptions muddy whether DNS resolution, a dropped SYN packet, or a TCP RST refusal failed.

Written purely with Python 3.12's standard library (`socket`, `ipaddress`, `concurrent.futures`, `time`), the tool validates hosts, verifies non-empty valid ports (`1..65535`) and positive timeouts ahead of DNS calls, deduplicates ports preserving requested order, and resolves DNS via `getaddrinfo()`.

When DNS resolves, `ThreadPoolExecutor` probes all requested ports concurrently and classifies each outcome into `CONNECTED`, `CONNECTION_REFUSED`, `TIMEOUT`, or `UNREACHABLE`.

3. Output & Source Specimen

Diagnostic Status Classification Matrix

01resolve_dns() -> RESOLVED | DNS_FAILURE (timed via time.perf_counter)
02CONNECTED <- TCP 3-way handshake succeeded within timeout
03CONNECTION_REFUSED <- Peer kernel replied with TCP RST (port closed)
04TIMEOUT <- SYN unanswered within timeout (filtered or host unreachable)
05SKIPPED_DNS_FAILURE <- Port checks skipped when initial DNS resolution failed
Deterministic probe statuses returned by TcpProbeResult / DnsCheckResultRun checks: pytest -v tests/test_diagnostics.py

4. Design Decisions & Challenges

Key Design Decisions

  • 01.Validated ports (`1..65535`) and positive timeouts before executing DNS queries.
  • 02.Parallelised per-port TCP checks via `ThreadPoolExecutor` so closed ports fail within one timeout window rather than N windows.
  • 03.Short-circuited probes to `SKIPPED_DNS_FAILURE` whenever hostname lookup fails.

Edge Cases Handled

  • Testing DNS NXDOMAIN, TCP RST, and socket timeout paths offline cleanly with `unittest.mock.patch`.

5. Testing & Limits

Automated Checks

Verified with 5 pytest checks in `tests/test_diagnostics.py` covering host validation, port/timeout guards, mocked DNS resolution, concurrent TCP states, `--fail-on-unreachable`, and CLI exit code 2 handling.

Current Limits & Next Steps

  • Operates at TCP connect level (`SOCK_STREAM`) rather than TLS certificate expiry checks.

6. Key Takeaways

What building Network Connection Tester reinforced

Validating preconditions ahead of network I/O and overlapping socket timeouts with thread pools cuts worst-case diagnostic wait time substantially.

  • — 5 pytest checks and Ruff lint pass cleanly in CI.
  • — Source hosted at github.com/16lv/tcp-dns-diagnostics.

Recent Activity

Latest repository updates

All changes for 16lv/tcp-dns-diagnostics

feat: add --fail-on-unreachable flag and clean exit code 2 error handling

Enabled CI health checks via --fail-on-unreachable and returned exit code 2 cleanly on invalid CLI parameters.

2026-11-244e919b9

feat: include top-level summary metrics in diagnostic report

Added summary totals (total_ports, open_ports, unreachable_ports, resolved_addresses) to build_diagnostic_report().

2026-11-237bd5bec

Add python -m net_diag runner and verify editable install in CI

Committed directly to tcp-dns-diagnostics (main) · A lightweight network tool built only with Python's standard library to time website address lookups and check whether server ports respond cleanly.

2026-11-21db17bee