def send(sock, addr, id, size, seq=1):
# Create an echo request
icmp_type = 8
icmp_code = 0
icmp_chksum = 0
icmp_id = id
icmp_seq = seq
header = struct.pack("bbHHh",
icmp_type, icmp_code, icmp_chksum, icmp_id, icmp_seq)
# The size of the packet must be large enough to accomodate the header and
# a timestamp
if size < struct.calcsize("bbHHhd"):
raise PacketSizeError()
# Create a chunk of data that is prepended by a timestamp so that we can
# determine how long it takes the packet to return.
timestamp = struct.pack("d", time.time())
data = timestamp + os.urandom(size - struct.caclsize("bbHHhd"))
# Now we can figure out the checksum
icmp_chksum = socket.htons(checksum(header + data))
# Construct the message using the checksum
header = struct.pack("bbHHh",
icmp_type, icmp_code, icmp_chksum, icmp_id, icmp_seq)
packet = header + data
# Send the request to the address
host = socket.gethostbyname(addr)
sock.sendto(packet, (host, 1))
评论列表
文章目录