【发布时间】:2020-02-14 03:41:20
【问题描述】:
我正在使用 python 从 ip 数据报中提取 procol(udp,tcp)。 但我的问题是解压缩从以太网帧获得的数据。 我为此使用此功能
def ipv4_head(data):
version_h = data[0]
version = version_h >> 4
header_length = (version_h & 15) * 4
ttl, proto, src, target = struct.unpack('! 8x B B 2x 4s 4s', raw_data[:20])
data = raw_data[header_length:]
return version, header_length, ttl, proto, src, target, data
我的问题是这行'''
struct.unpack('!8x B B 2x 4s 4s', raw_data[:20])
我有一个错误
struct.error: unpack 需要 20 字节的缓冲区
我尝试了很多想法,这个也给出了相同的结果
struct.unpack("!BBHHHBBH4s4s", raw_data)
有其他功能但还是一样
def ipv4_head(raw_data):
store=struct.unpack("!BBHHHBBH4s4s", raw_data)
src_ip=socket.inet_ntoa(store[8])
dst_ip=socket.inet_ntoa(store[9])
protocol=store[6]
return src_ip,dst_ip, protocol
不过都一样,我用的是python 3。
我也有兴趣,用另一种方法绕过 struct.unpack。 很多天我都在为这个错误而苦苦挣扎,但我在互联网上一无所获。
先谢谢各位了
【问题讨论】: