MultiPing Library
The `multiping` library is a pure Python implementation for sending and receiving ICMP echo requests (ping) to monitor IP addresses. It allows for concurrent pinging of multiple hosts, providing response times and identifying unreachable hosts. The current version is 1.1.2. Releases are infrequent but address compatibility and bug fixes, indicating a stable, low-maintenance project.
Common errors
-
OSError: [Errno 1] Operation not permitted
cause The Python process does not have the necessary permissions to create raw sockets required for sending ICMP packets.fixRun the script with elevated privileges: `sudo python your_script.py` on Linux/macOS, or as an administrator on Windows. Ensure no firewall is blocking outbound ICMP traffic. -
socket.error: [Errno 1] Operation not permitted
cause Same as `OSError`, indicates insufficient permissions to perform raw socket operations.fixRun the script with elevated privileges: `sudo python your_script.py` on Linux/macOS, or as an administrator on Windows. Check firewall rules. -
ModuleNotFoundError: No module named 'multiping'
cause The `multiping` library has not been installed in the current Python environment, or the environment is not active.fixInstall the library using pip: `pip install multiping`. If using a virtual environment, ensure it is activated before installation and execution.
Warnings
- gotcha Sending raw ICMP packets often requires elevated privileges (root on Linux/macOS, administrator on Windows). Without them, you will likely encounter `PermissionError` or `OSError: [Errno 1] Operation not permitted`.
- gotcha The `receive()` method blocks for the specified timeout, collecting responses. If the timeout is too short, you might miss late responses. If it's too long, your program might wait unnecessarily.
- gotcha Resolving hostnames (e.g., 'google.com') occurs synchronously when `MultiPing` is initialized. If you have many hostnames or slow DNS servers, this can delay the start of pinging. Unresolvable hostnames will lead to them being silently dropped from the list of hosts to ping.
Install
-
pip install multiping
Imports
- MultiPing
from multiping import MultiPing
Quickstart
from multiping import MultiPing
import time
# List of hosts to ping
hosts = ['127.0.0.1', 'google.com', 'nonexistent.domain', '8.8.8.8']
try:
# Create a MultiPing instance with a list of hosts
# Optionally specify a port (defaults to 1 for ICMP) and IPv6 (False by default)
mp = MultiPing(hosts, timeout=1, retry=1, verbose=False)
# Send the ICMP echo requests
# This sends packets in a non-blocking way
mp.send()
# Wait for responses for up to 1 second
# receive() returns responses, and a list of hosts that did not respond within the timeout
responses, no_responses = mp.receive(1.0)
print("--- Ping Results ---")
for addr, rtt in responses.items():
print(f"Host: {addr}, RTT: {rtt*1000:.2f} ms")
if no_responses:
print("\n--- No Response From ---")
for addr in no_responses:
print(f"Host: {addr}")
except PermissionError:
print("Error: Permission denied. Running ping often requires root/administrator privileges.")
print("Try running with 'sudo python your_script.py' or as an administrator on Windows.")
except Exception as e:
print(f"An unexpected error occurred: {e}")