【问题标题】:python unable to extract protocol from ethernet frame 'struct.error: unpack requires a buffer of 20 bytes'python无法从以太网帧中提取协议'struct.error:解包需要20字节的缓冲区'
【发布时间】: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。 很多天我都在为这个错误而苦苦挣扎,但我在互联网上一无所获。

先谢谢各位了

【问题讨论】:

    标签: python udp ethernet


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2020-05-11
      • 1970-01-01
      • 2022-10-24
      • 2021-10-06
      • 2020-10-24
      • 1970-01-01
      • 1970-01-01
      • 2013-03-26
      • 1970-01-01
      相关资源
      最近更新 更多