ICMP Ping/ICMPPing.py
def ping(host, count_num="4", time_out="1"):
    """Print the result to the console.

    This function will print the IP address of the host, byte size, latency
    and TTL of a packet or handle an exception after each ping.

    Args:
        :param host: The website or IP address that we want to test latency.
        :param count_num: the total number of the network delay test.
        :param timeout: configurable timeout, set using an optional argument.
    """
    # 1. Look up hostname, resolving it to an IP address.
    ip_addr = socket.gethostbyname(host)
    successful_list = list()
    lost = 0
    error = 0
    bytes = 32
    count = int(count_num)
    timeout = int(time_out)
    timeout_start = 0
    head = False
    timedout_mark = False
    for i in range(count):  # i is value of sequence.
        # Print header.
        if head is False:
            if host == ip_addr:
                print("Pinging {ipAddr} with {bytes} of data:"
                      .format(ipAddr = ip_addr, bytes = bytes))
            else:
                print("Pinging {hostName} [{ipAddr}] with {bytes} of data:"
                      .format(hostName = host, ipAddr = ip_addr, bytes = bytes))
            head = True
        try:
            # 2. Call doOnePing function.
            ping_delay = do_one_ping(ip_addr, timeout, i)
            # 3. Print out the returned delay information.
            if ping_delay == 0 or ping_delay == 1:
                # To get my laptop's IP address.
                ip_addr = socket.gethostbyname(socket.gethostname())
                print("Reply from {ipAdrr}: ".format(ipAdrr = ip_addr), end = "")
                result = "Destination host unreachable." if ping_delay == 0 else \
                         "Destination net unreachable."
                print(result)
                error += 1
            else:
                bytes, delay_time, ttl = ping_delay[0], int(ping_delay[1] * 1000), \
                                         ping_delay[2]
                print("Reply from {ipAdrr}: ".format(ipAdrr = ip_addr), end = "")
                # If a packet can be received successfully,
                # the list could append this delay time.
                successful_list.append(delay_time)
                # if delay_time < 1m, then we will get 0 ms.
                if delay_time == 0:
                    print("bytes = {bytes} time < 1ms TTL = {ttl}."
                          .format(bytes = bytes, ttl = ttl))
                else:
                    print("bytes = {bytes} time = {delayTime}ms TTL = {ttl}."
                          .format(bytes = bytes, delayTime = delay_time, ttl = ttl))
        except TimeoutError:  # timeout type
            lost += 1
            print("Request timed out.")
            # If it's not always a timeout case,
            # we might need to calculate the maximum latency.
            if timedout_mark is False:
                timedout_mark = True
        time.sleep(1)  # Every second.
    #  4. Continue this process until stopped.
    print("\nPing statistics for {ipAddr}:\n".format(ipAddr = ip_addr), end = "")
    print("\tPacket: Sent = {c}, Received = {receiveAmt}, lost = {lostAmt} ({lostPercent}% loss)."
        .format(c = count, receiveAmt = count - lost, lostAmt = lost, lostPercent = int((lost / 4) * 100)))
    if lost < count and error <= 0:
        sortResult = ping_statistics(successful_list)
        mini_delay, max_delay, avg_delay = sortResult[0], sortResult[1], sortResult[2]
        max_delay = max_delay if timedout_mark is False else timeout*1000
        print("Approximate round trip times in milli - seconds:\n", end = "")
        print("\tMinimum = {mini}ms, Maximum = {max}ms, Average = {ave}ms.\n"
            .format(mini = mini_delay, max = max_delay, ave = avg_delay))
Print the result to the console.

    This function will print the IP address of the host, byte size, latency
    and TTL of a packet or handle an exception after each ping.

    Args:
        :param host: The website or IP address that we want to test latency.
        :param count_num: the total number of the network delay test.
        :param timeout: configurable timeout, set using an optional argument.
    
Print the result to the console.
This function will print the IP address of the host, byte size, latency
and TTL of a packet or handle an exception after each ping.
Print the result to the console . This function will print the IP address of the host byte size latency and TTL of a packet or handle an exception after each ping .
def ping(host, count_num="4", time_out="1"):
    ip_addr = socket.gethostbyname(host)
    successful_list = list()
    lost = 0
    error = 0
    bytes = 32
    count = int(count_num)
    timeout = int(time_out)
    timeout_start = 0
    head = False
    timedout_mark = False
    for i in range(count):  
        if head is False:
            if host == ip_addr:
                print("Pinging {ipAddr} with {bytes} of data:"
                      .format(ipAddr = ip_addr, bytes = bytes))
            else:
                print("Pinging {hostName} [{ipAddr}] with {bytes} of data:"
                      .format(hostName = host, ipAddr = ip_addr, bytes = bytes))
            head = True
        try:
            ping_delay = do_one_ping(ip_addr, timeout, i)
            if ping_delay == 0 or ping_delay == 1:
                ip_addr = socket.gethostbyname(socket.gethostname())
                print("Reply from {ipAdrr}: ".format(ipAdrr = ip_addr), end = "")
                result = "Destination host unreachable." if ping_delay == 0 else \
                         "Destination net unreachable."
                print(result)
                error += 1
            else:
                bytes, delay_time, ttl = ping_delay[0], int(ping_delay[1] * 1000), \
                                         ping_delay[2]
                print("Reply from {ipAdrr}: ".format(ipAdrr = ip_addr), end = "")
                successful_list.append(delay_time)
                if delay_time == 0:
                    print("bytes = {bytes} time < 1ms TTL = {ttl}."
                          .format(bytes = bytes, ttl = ttl))
                else:
                    print("bytes = {bytes} time = {delayTime}ms TTL = {ttl}."
                          .format(bytes = bytes, delayTime = delay_time, ttl = ttl))
        except TimeoutError:  
            lost += 1
            print("Request timed out.")
            if timedout_mark is False:
                timedout_mark = True
        time.sleep(1)  
    print("\nPing statistics for {ipAddr}:\n".format(ipAddr = ip_addr), end = "")
    print("\tPacket: Sent = {c}, Received = {receiveAmt}, lost = {lostAmt} ({lostPercent}% loss)."
        .format(c = count, receiveAmt = count - lost, lostAmt = lost, lostPercent = int((lost / 4) * 100)))
    if lost < count and error <= 0:
        sortResult = ping_statistics(successful_list)
        mini_delay, max_delay, avg_delay = sortResult[0], sortResult[1], sortResult[2]
        max_delay = max_delay if timedout_mark is False else timeout*1000
        print("Approximate round trip times in milli - seconds:\n", end = "")
        print("\tMinimum = {mini}ms, Maximum = {max}ms, Average = {ave}ms.\n"
            .format(mini = mini_delay, max = max_delay, ave = avg_delay))
def ping ( host , count_num = "4" , time_out = "1" ) : ip_addr = socket . gethostbyname ( host ) successful_list = list ( ) lost = 0 error = 0 bytes = 32 count = int ( count_num ) timeout = int ( time_out ) timeout_start = 0 head = False timedout_mark = False for i in range ( count ) : if head is False : if host == ip_addr : print ( "Pinging {ipAddr} with {bytes} of data:" . format ( ipAddr = ip_addr , bytes = bytes ) ) else : print ( "Pinging {hostName} [{ipAddr}] with {bytes} of data:" . format ( hostName = host , ipAddr = ip_addr , bytes = bytes ) ) head = True try : ping_delay = do_one_ping ( ip_addr , timeout , i ) if ping_delay == 0 or ping_delay == 1 : ip_addr = socket . gethostbyname ( socket . gethostname ( ) ) print ( "Reply from {ipAdrr}: " . format ( ipAdrr = ip_addr ) , end = "" ) result = "Destination host unreachable." if ping_delay == 0 else "Destination net unreachable." print ( result ) error += 1 else : bytes , delay_time , ttl = ping_delay [ 0 ] , int ( ping_delay [ 1 ] * 1000 ) , ping_delay [ 2 ] print ( "Reply from {ipAdrr}: " . format ( ipAdrr = ip_addr ) , end = "" ) successful_list . append ( delay_time ) if delay_time == 0 : print ( "bytes = {bytes} time < 1ms TTL = {ttl}." . format ( bytes = bytes , ttl = ttl ) ) else : print ( "bytes = {bytes} time = {delayTime}ms TTL = {ttl}." . format ( bytes = bytes , delayTime = delay_time , ttl = ttl ) ) except TimeoutError : lost += 1 print ( "Request timed out." ) if timedout_mark is False : timedout_mark = True time . sleep ( 1 ) print ( "\nPing statistics for {ipAddr}:\n" . format ( ipAddr = ip_addr ) , end = "" ) print ( "\tPacket: Sent = {c}, Received = {receiveAmt}, lost = {lostAmt} ({lostPercent}% loss)." . format ( c = count , receiveAmt = count - lost , lostAmt = lost , lostPercent = int ( ( lost / 4 ) * 100 ) ) ) if lost < count and error <= 0 : sortResult = ping_statistics ( successful_list ) mini_delay , max_delay , avg_delay = sortResult [ 0 ] , sortResult [ 1 ] , sortResult [ 2 ] max_delay = max_delay if timedout_mark is False else timeout * 1000 print ( "Approximate round trip times in milli - seconds:\n" , end = "" ) print ( "\tMinimum = {mini}ms, Maximum = {max}ms, Average = {ave}ms.\n" . format ( mini = mini_delay , max = max_delay , ave = avg_delay ) )
Print the result to the console.
Print the result to the console .
"""Print the result to the console.

    This function will print the IP address of the host, byte size, latency
    and TTL of a packet or handle an exception after each ping.

    Args:
        :param host: The website or IP address that we want to test latency.
        :param count_num: the total number of the network delay test.
        :param timeout: configurable timeout, set using an optional argument.
    """ # 1. Look up hostname, resolving it to an IP address. # i is value of sequence. # Print header. # 2. Call doOnePing function. # 3. Print out the returned delay information. # To get my laptop's IP address. # If a packet can be received successfully, # the list could append this delay time. # if delay_time < 1m, then we will get 0 ms. # timeout type # If it's not always a timeout case, # we might need to calculate the maximum latency. # Every second. #  4. Continue this process until stopped.
host
count_num
time_out
param timeout: configurable timeout, set using an optional argument.
param timeout : configurable timeout set using an optional argument .