import time
import array
def jiaoyan(pack):
if len(pack)%2==1:
pack+=b'\0'
words=array.array('h',pack)
sum=0
for word in words:
sum += word
sum = (sum >> 16) + (sum & 0xffff)
sum = (sum >> 16) + (sum & 0xffff)
return (~sum) & 0xffff
icmp=socket.socket(socket.AF_INET,socket.SOCK_RAW,socket.IPPROTO_ICMP)
icmp.settimeout(3)
header = struct.pack('bbHHh', 8,0,0,1234, 1)
data=struct.pack('5s',b'haasd')
print(jiaoyan(header+data))
icmp.sendto(header+data, ('127.0.0.1', 1))
我想完成一个icmp的封装 然后发给自己 我用回文地址127.0.0.1,来发给自己。封装icmp的时候,我用jiaoyan方法整了个产生校验码位的函数。初始的校验码为0.这是我的思路,但是最后发icmp包的时候出错了 。好奇怪