import struct,array
import socket,time
service=socket.socket(socket.AF_INET,socket.SOCK_RAW,socket.IPPROTO_ICMP)
service.settimeout(3)
head=struct.pack('bbHHh',8,0,0,1,1)
data=struct.pack('h',4)
bao=head+data
def jiaoyan(bao):
sum=0
if len(bao)&1:
bao=bao+b'\0'
words=array.array('h',bao)
for word in words:
sum+=word
word=word>>16+word&0xffff
word = word >> 16 + word & 0xffff
return ~sum&0xffff
hhhhhhh=jiaoyan(bao)
head=struct.pack('bbHHh',8,0,hhhhhhh,1,1)
bao=head+data
service.sendto(bao,('127.0.0.1',2000))
data,add=service.recvfrom(100)
print(data)
service.close()
这是我写的完美的icmp包,然后ping主机的代码,但是运行后数据包超时。。