def ping(addr, size=16):
# Open a raw socket to send the request. NB: you need to be root to have
# permission to open a raw socket.
sock = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_ICMP)
# Generate a positive, random 16-bit integer to serve as the id for this
# ping
id = random.randint(1, 2**16 - 1)
# Send the message and wait for the reply
send(sock, addr, id, size)
duration = yield from recv(sock, id)
return duration
评论列表
文章目录