def ping_once(ip_addr, timeout):
"""
return either delay (in second) or none on timeout.
"""
# Translate an Internet protocol name to a constant suitable for
# passing as the (optional) third argument to the socket() function.
# This is usually only needed for sockets opened in “raw” mode.
icmp = socket.getprotobyname('icmp')
try:
# socket.socket([family[, type[, proto]]])
# Create a new socket using the given address family(default: AF_INET),
# socket type(SOCK_STREAM) and protocol number(zero or may be omitted).
my_socket = socket.socket(socket.AF_INET, socket.SOCK_RAW, icmp)
except socket.error:
raise
# Return the current process id.
# int: 0xFFFF = -1, unsigned int: 65535
my_ID = os.getpid() & 0xFFFF
send_ping(my_socket, ip_addr, my_ID)
delay = receive_ping(my_socket, my_ID, timeout)
my_socket.close()
return delay
评论列表
文章目录