无法解压

Unable to unpack

我在解析 BLE 数据包时出现以下错误。有什么解决方法的建议吗?

数据包 b'\x04>+\x02\x01\x03\x01\xd7\xd3A\xc9\xae\xf5\x1f\x02\x01\x06\x03\x03\xaa\xfe\x17\x16\xaa\xfe\x00\xd8\x8b\x9c\xc7<:\xe7G\xefe\xbc\x00\x00\x00\x00\x15\xd1\x00\x00\xaf'

Traceback (most recent call last):
File "BluetoothWiliot.py", line 95, in <module>
    dataString = parse_events(sock, 100)
File "BluetoothWiliot.py", line 47, in parse_events
    print(struct.unpack("B", bytes(pkt[3])))
**struct.error: unpack requires a buffer of 1 bytes**


Traceback (most recent call last):
File "BluetoothWiliot.py", line 95, in <module>
    dataString = parse_events(sock, 100)
File "BluetoothWiliot.py", line 47, in parse_events
    print(struct.unpack("B", pkt[3]))

**TypeError: a bytes-like object is required, not 'int'**

int.from_bytes 通常是我在这些情况下的工作方式,但有多种选择:

$ python3
Python 3.7.3 (default, Jul 25 2020, 13:03:44) 
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> pkt = b'\x04>+\x02\x01\x03\x01\xd7\xd3A\xc9\xae\xf5\x1f\x02\x01\x06\x03\x03\xaa\xfe\x17\x16\xaa\xfe\x00\xd8\x8b\x9c\xc7<:\xe7G\xefe\xbc\x00\x00\x00\x00\x15\xd1\x00\x00\xaf'
>>> pkt[3]
2
>>> int.from_bytes([pkt[3]], byteorder='little', signed=False)
2
>>> struct.unpack_from('B', pkt, 3)
(2,)
>>> struct.unpack('B',pkt[3:3+1])
(2,)
>>> struct.unpack('B', bytes([pkt[3]]))
(2,)