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.
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
Run checks: pytest -v tests/test_diagnostics.py4. 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
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.
feat: include top-level summary metrics in diagnostic report
Added summary totals (total_ports, open_ports, unreachable_ports, resolved_addresses) to build_diagnostic_report().
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.