Python ping library and CLI tool with ICMP, TCP, HTTP, and DNS fallback
Project description
PyNetPing
PyNetPing is a Python library and CLI tool for host reachability testing. It supports ICMP, TCP, HTTP, and DNS pings over IPv4 and IPv6, with automatic fallback if one method fails.
Features
-
Ping hosts using multiple protocols:
-
ICMP (IPv4 & IPv6)
-
TCP (IPv4 & IPv6)
-
HTTP (IPv4 & IPv6)
-
DNS (IPv4 & IPv6)
-
-
Automatic fallback if a protocol fails: ICMP → TCP → HTTP → DNS
-
Automatic IP version detection based on host address
-
Detailed Statistics:
-
Packets sent / received
-
Packet loss (%)
-
Minimum, maximum, and average latency (ms)
-
Jitter (ms) – variation in latency
-
-
Concurrent Pings: Ping multiple hosts at the same time using the async engine.
-
Output Options:
-
JSON output for easy integration
-
Save results as CSV files
-
-
CLI Tool: Use PyNetPing directly from the command line with all the above features.
Requirements
-
Python 3.10 or higher
-
Privileges:
-
ICMP requires elevated privileges:
-
Linux / macOS: run as root
-
Windows: run as Administrator
-
-
-
If ICMP is unavailable, PyNetPing automatically falls back to TCP, HTTP, or DNS
IPv6 Notes
-
All ping methods support IPv6
-
IPv6 availability depends on:
-
Network configuration
-
OS support
-
-
If IPv6 is unreachable, fallback mechanisms apply automatically
Installation
You can install PyNetPing using pip or pip3:
pip3 install pynetping
# or
pip install pynetping
Using PyNetPing as a Library
The ping() Function
PyNetPing exposes a single main function for reachability testing: ping().
It automatically selects the best available method and falls back if one method fails.
Function Signature:
ping(
host: str, # required
count: int = 4,
timeout: float = 1.0,
port: int = 80,
rate: float = 10.0,
use_dns: bool = False
) -> PingResult
ping() Arguments:
| Argument | Required | Default | Description |
|---|---|---|---|
host |
Yes | — | Target hostname or IP address (IPv4 or IPv6) |
count |
No | 4 |
Number of ping attempts |
timeout |
No | 1.0 |
Timeout per request in seconds |
port |
No | 80 |
TCP port used only for TCP fallback |
rate |
No | 10.0 |
Requests per second (rate limiting) |
use_dns |
No | False |
Enable DNS ping fallback if other methods fail |
Examples
Basic Ping Usage (Library)
The simplest way to check if a host is reachable.
from PyNetPing import ping
result = ping("8.8.8.8")
print(result)
- Automatically selects IPv4 or IPv6
- Uses ICMP if available
- Falls back to TCP → HTTP if needed
Advanced Usage (Library)
Custom Ping Count, Timeout, and Rate
from PyNetPing import ping
result = ping(
host="8.8.8.8",
count=10,
timeout=2.0,
rate=5.0
)
print(result)
Explanation:
-
Sends 10 ping requests
-
Waits up to 2 seconds per request
-
Limits sending speed to 5 requests per second
Enable DNS Fallback (Library) (Optional)
DNS ping is disabled by default and only used if explicitly enabled.
from PyNetPing import ping
result = ping(
host="8.8.8.8",
use_dns=True
)
print(result)
- DNS is attempted only if ICMP, TCP, and HTTP fail
Specify TCP Port for Fallback (Library)
TCP fallback uses port 80 by default. You can override it:
from PyNetPing import ping
result = ping(
host="8.8.8.8",
port=443
)
print(result)
- TCP fallback will try port 443 instead of 80
Print Output as JSON (Library)
from PyNetPing import ping
from PyNetPing import to_json
result = ping("8.8.8.8")
json_data = to_json([result])
print(json_data)
Save Output as CSV (Library)
from PyNetPing import ping
from PyNetPing import to_csv
result = ping("8.8.8.8")
to_csv([result], "results.csv")
Ping Multiple Hosts (Async) (Library)
import asyncio
from PyNetPing import ping_hosts
hosts = ["8.8.8.8", "1.1.1.1", "google.com"]
results = asyncio.run(
ping_hosts(
hosts,
count=3, # number of ping requests per host (default 4)
timeout=1.5, # max wait per ping in seconds (default 1.0)
port=443, # Fallback TCP ping port if ICMP fails (default: 80)
limit=50 # max number of concurrent pings (default 100)
)
)
for r in results:
print(r)
Using PyNetPing as a CLI Tool
PyNetPing also works as a command-line tool.
CLI automatically selects the best protocol: ICMP → TCP → HTTP → DNS (if --dns is enabled).
Run as root/admin to use ICMP ping fully. On Linux/macOS.in windows run as administrator:
Exit codes for automation:
| Exit Code | Meaning |
|---|---|
| 0 | Success (all pings received) |
| 1 | Partial packet loss |
| 2 | Host unreachable |
Basic Usage:
python3 -m PyNetPing 8.8.8.8
Options:
| Option | Type | Default | Description |
|---|---|---|---|
| host | str | — | Target host to ping (required) |
| -c, --count | int | 4 | Number of ping requests to send |
| -t, --timeout | float | 1.0 | Timeout in seconds for each request |
| -p, --port | int | 80 | Fallback TCP ping port if ICMP fails |
| -r, --rate | float | 10.0 | Maximum requests per second |
| --dns | flag | False | Use DNS ping if other protocols fail |
| --json | flag | False | Output results in JSON format |
| --csv | str | — | Save output to the specified CSV file |
Examples:
- Basic ping:
python3 -m PyNetPing 8.8.8.8
- Ping with 10 Requests, 2-Second Timeout, and Custom Rate
python3 -m PyNetPing 8.8.8.8 -c 10 -t 2 -r 5
Explanation:
-
-c 10→ Send 10 ping requests -
-t 2→ Wait up to 2 seconds for each request -
-r 5→ Send 5 requests per second -
8.8.8.8→ Target host
- Ping with DNS Fallback
python3 -m PyNetPing 8.8.8.8 --dns
--dnsenables DNS ping if ICMP, TCP, and HTTP fail.
- print output in JSON format:
python3 -m PyNetPing 8.8.8.8 --json
- Save output to CSV file:
python3 -m PyNetPing 8.8.8.8 --csv output.csv
- Ping with custom TCP fallback port
python3 -m PyNetPing 8.8.8.8 -p 443
-
Attempts ICMP ping first.
-
If ICMP fails, falls back to TCP ping.
-
TCP ping uses the specified port (
-p/--port). -
Default TCP fallback port is 80 if not specified.
License
This project is licensed under the MIT License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pynetping-1.0.2.tar.gz.
File metadata
- Download URL: pynetping-1.0.2.tar.gz
- Upload date:
- Size: 11.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
eddeae0d0724e43e96deeb9ab2ff0692a768e3fd192218597b118c2306b44821
|
|
| MD5 |
3b2852864b0cd56411074054ff56e2f2
|
|
| BLAKE2b-256 |
479e7d0470e1c0c747dc3459b8ed68ece3b9803a6fda303c8a3d9ee93064de9c
|
File details
Details for the file pynetping-1.0.2-py3-none-any.whl.
File metadata
- Download URL: pynetping-1.0.2-py3-none-any.whl
- Upload date:
- Size: 13.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d226ff6684fa3fde524b0b0d31ca40d20878e15f7ee3b18975f353d4707a8d29
|
|
| MD5 |
ca2722e3b1bba47595b29132904bcd4f
|
|
| BLAKE2b-256 |
43c714dd8bb5ed6a14036b43f218c5ee17987c1c76e9ab64db535f7c25f4a5af
|