Registry / http-networking / icmplib

icmplib

JSON →
library3.0.4pypypi✓ verified 20d ago

icmplib (current version 3.0.4) is a pure Python library designed for easily forging ICMP packets and building network tools like ping and traceroute without relying on external dependencies. It is actively developed, offering both synchronous and asynchronous APIs, and provides cross-platform compatibility for Linux, macOS, and Windows, supporting both IPv4 and IPv6. It requires Python 3.7 or later.

pip install icmplib
INSTALL
IMPORT
SIG · ICMPLIB
I
icmplib
http-networkingpythonv3.0.4
Install
1.5s avg
Import
211ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v3.0.4 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.226s · 18MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.5s · import 0.196s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

async_ping
from icmplib import async_ping
from icmplib import ping
As of v3.0.0, the library is primarily asynchronous. The `ping` function is still available but rewritten and now defaults to an asynchronous implementation if not explicitly called from an async context. For new async projects, `async_ping` is the recommended entry point.
Host
from icmplib import Host
SocketPermissionError
from icmplib import SocketPermissionError

This example demonstrates how to perform an asynchronous ICMP ping to a target address and report its status and average round-trip time. It uses the `async_ping` function, which is the recommended way to interact with the library since version 3.0.0.

import asyncio from icmplib import async_ping async def check_host(address): try: host = await async_ping(address, count=4, interval=0.2, timeout=2) if host.is_alive: print(f"{host.address} is UP! Latency: {host.avg_rtt:.2f} ms") else: print(f"{host.address} is DOWN.") except Exception as e: print(f"Error checking {address}: {e}") if __name__ == "__main__": target_address = "example.com" # Replace with an IP address or hostname asyncio.run(check_host(target_address))
Debug
Known issues
breakingVersion 3.0.0 introduced a significant breaking change by making the library primarily asynchronous. All core functions like `ping`, `multiping`, and `traceroute` were rewritten to be non-blocking.
fix
Migrate synchronous code using `ping`, `multiping`, or `traceroute` to their `async_` counterparts (e.g., `async_ping`) and run them within an `asyncio` event loop.
affects: >=3.0.0
gotchaWhen using `icmplib` on Unix-like systems (Linux, macOS), creating raw ICMP sockets often requires root privileges, which can raise a `SocketPermissionError`.
fix
Run your Python script with `sudo` (e.g., `sudo python your_script.py`) or initialize sockets with `privileged=False` if your operating system configuration allows unprivileged ICMP sockets. The `ping`, `multiping`, and `traceroute` functions accept a `privileged` parameter.
affects: All versions
breakingVersion 2.0.0 introduced a 'new library architecture' and changed how the library handles root privileges, adding the ability to use it without root privileges by setting the `privileged` parameter.
fix
Review existing code written for 1.x to ensure compatibility with the new architecture and explicitly handle the `privileged` parameter if non-root operation is desired.
affects: >=2.0.0
gotchaAs of version 3.0.2, the `payload` property of an `ICMPRequest` object now returns a random value if the payload is not explicitly defined, instead of `None`.
fix
If your application relies on `payload` being `None` when not set, adjust your logic to check for explicitly empty payloads or handle random data. To define an empty payload, set `payload=b''`.
affects: >=3.0.2
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'icmplib'
The 'icmplib' library has not been installed in the Python environment currently being used.
fix
Install the library using pip: `pip install icmplib` or `pip3 install icmplib`.
icmplib.exceptions.SocketPermissionError: Root privileges are required to create the socket
On Unix-like systems (Linux, macOS), creating raw ICMP sockets typically requires root privileges, which the current user lacks.
fix
Run the Python script with `sudo` (e.g., `sudo python your_script.py`). Alternatively, you can allow the Python executable to manipulate raw network packets without `sudo` by setting file capabilities: `sudo setcap cap_net_raw+ep $(realpath $(which python3))` (this affects all Python scripts). For `ping` and `multiping` functions, you might be able to use `privileged=False` (e.g., `ping('1.1.1.1', privileged=False)`) if your OS kernel is configured to handle ICMP headers in unprivileged mode (e.g., by setting `sudo sysctl -w net.ipv4.ping_group_range='0 2147483647'` on Linux), though this may not be suitable for `traceroute`.
OSError: [WinError 10013] An attempt was made to access a socket in a way forbidden by its access permissions.
On Windows, creating raw sockets for ICMP operations often requires administrator privileges, or the attempt might be blocked by firewall or security software.
fix
Run the Python script or your IDE as an administrator. Ensure that no firewall rules are blocking the application from creating raw sockets.
icmplib.exceptions.NameLookupError: The requested name does not exist or cannot be resolved
The hostname or Fully Qualified Domain Name (FQDN) provided to an `icmplib` function could not be resolved to an IP address by the system's DNS resolver.
fix
Verify that the hostname or FQDN is spelled correctly and is reachable on the network. Ensure your system's DNS settings are correct. Consider using an IP address directly instead of a hostname if the issue persists.
Upgrade
Version history
3.0.4latest on PyPI · released Oct 10, 2023
Audit
Dependencies

No dependency data recorded yet.

Agent activity
8 hits · last 30 days
node
6
Resources
icmplib — pip install icmplib · libregistry