python无法从以太网帧';struct.error:unpack需要20字节的缓冲区';

python无法从以太网帧';struct.error:unpack需要20字节的缓冲区';,python,udp,ethernet,Python,Udp,Ethernet,我正在使用python从ip数据报中提取过程(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_d

我正在使用python从ip数据报中提取过程(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
我的问题是这行''

结构解包(“!8x B 2x 4s 4s”,原始数据[:20])

我有个错误

struct.error:解包需要20字节的缓冲区

我试了很多想法,这一个也给出了同样的结果

解包结构(“!BBHHBBH4S”,原始数据)

具有其他功能,但仍然相同

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
但它是一样的,我使用Python3

我也有兴趣使用另一种方法绕过struct.unpack。 好几天我都在纠结这个错误,但我在网上什么也没找到


提前感谢各位

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

你能试着打印(len(原始数据[:20])吗?如果
raw_data
小于20个字节,则表达式将不够长