python无法从以太网帧'struct.error中提取协议:解压缩需要20个字节的缓冲区'

问题描述 投票:0回答:1

我正在使用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:解压缩需要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。好几天我都在为这个错误而苦苦挣扎,但我在互联网上什么也没找到。

先谢谢大家

python udp ethernet
1个回答
0
投票

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

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

© www.soinside.com 2019 - 2024. All rights reserved.