Pythonping

1.1.4 · active · verified Thu Apr 16

Pythonping is an active Python library (current version 1.1.4) that provides a simple and modular way to send ICMP probes to remote devices, similar to the `ping` utility available in most operating systems. It allows developers to perform network reachability tests directly from Python scripts, with options to customize packet size, timeout, interval, and collect detailed response statistics. The library maintains a steady release cadence with improvements and bug fixes.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart pings Google's public DNS server (8.8.8.8) four times, prints detailed output for each response, and then summarizes the session with statistics like packet loss and round-trip times. It includes error handling for common permission issues.

import os
from pythonping import ping

# Ping a common public DNS server (e.g., Google DNS) or a local host.
# Note: Pinging often requires administrative privileges (root/sudo on Linux/macOS, or elevated command prompt on Windows)
# due to raw socket access. If you encounter 'PermissionError', try running your script with appropriate permissions.

try:
    print("\nPinging 8.8.8.8 with 4 packets...")
    response_list = ping('8.8.8.8', count=4, timeout=1, verbose=True)
    
    print("\n--- Ping Results ---")
    for response in response_list:
        if response.is_success:
            print(f"Reply from {response.address}, {response.bytes_received} bytes in {response.time_elapsed:.2f}ms")
        else:
            print(f"Request to {response.address} timed out or failed.")

    # Access statistics available from v1.1.3+
    if hasattr(response_list, 'stats_packets_sent'):
        print(f"\nPackets Sent: {response_list.stats_packets_sent}")
        print(f"Packets Received: {response_list.stats_packets_returned}")
        print(f"Packet Loss Ratio: {response_list.stats_lost_ratio:.2%}")
        print(f"Min RTT: {response_list.rtt_min:.2f}ms, Avg RTT: {response_list.rtt_avg:.2f}ms, Max RTT: {response_list.rtt_max:.2f}ms")

except PermissionError as e:
    print(f"Error: {e}. You might need to run this script with administrative privileges (e.g., sudo on Linux/macOS, or as Administrator on Windows).")
except Exception as e:
    print(f"An unexpected error occurred: {e}")

view raw JSON →