繁体   English   中英

python无法从以太网帧“struct.error: unpack requires a buffer of 20 bytes”中提取协议

[英]python unable to extract protocol from ethernet frame 'struct.error: unpack requires a buffer of 20 bytes'

我正在使用 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 BB 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。 很多天我都在为这个错误而苦苦挣扎,但我在互联网上什么也没找到。

提前谢谢各位

该错误告诉您缓冲区(第二个参数)的长度不是 20 个字节。

你能试试print(len(raw_data[:20]))吗? 如果raw_data短于 20 个字节,则您的表达式将不够长。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM