使用 struct.unpack_from(fmt=,buffer=,offfset=)
该函数可以将缓冲区buffer
中的内容在按照指定的格式fmt='somenformat'
,从偏移量为offset=numb
的位置开始进行读取。返回的是一个对应的元组tuple
,一般使用的场景是从一个二进制或者其他文件中读取的内容进行解析操作。
Format Strings
Format strings are the mechanism used to specify the expected layout when packing and unpacking data. They are built up from Format Characters, which specify the type of data being packed/unpacked. In addition, there are special characters for controlling the Byte Order, Size, and Alignment.
Byte Order, Size, and Alignment
By default, C types are represented in the machine’s native format and byte order, and properly aligned by skipping pad bytes if necessary (according to the rules used by the C compiler).
Alternatively, the first character of the format string can be used to indicate the byte order, size and alignment of the packed data, according to the following table:
Character |
Byte order |
Size |
Alignment |
---|---|---|---|
|
native |
native |
native |
|
native |
standard |
none |
|
little-endian |
standard |
none |
|
big-endian |
standard |
none |
|
network (= big-endian) |
standard |
none |
If the first character is not one of these, '@'
is assumed.
Native byte order is big-endian or little-endian, depending on the host system. For example, Intel x86 and AMD64 (x86-64) are little-endian; Motorola 68000 and PowerPC G5 are big-endian; ARM and Intel Itanium feature switchable endianness (bi-endian). Use sys.byteorder
to check the endianness of your system.
Native size and alignment are determined using the C compiler’s sizeof
expression. This is always combined with native byte order.
Standard size depends only on the format character; see the table in the Format Characters section.
Note the difference between '@'
and '='
: both use native byte order, but the size and alignment of the latter is standardized.
The form '!'
is available for those poor souls who claim they can’t remember whether network byte order is big-endian or little-endian.
There is no way to indicate non-native byte order (force byte-swapping); use the appropriate choice of '<'
or '>'
.
Notes:
-
Padding is only automatically added between successive structure members. No padding is added at the beginning or the end of the encoded struct.
-
No padding is added when using non-native size and alignment, e.g. with ‘<’, ‘>’, ‘=’, and ‘!’.
-
To align the end of a structure to the alignment requirement of a particular type, end the format with the code for that type with a repeat count of zero. See Examples.
Format Characters
Format characters have the following meaning; the conversion between C and Python values should be obvious given their types. The ‘Standard size’ column refers to the size of the packed value in bytes when using standard size; that is, when the format string starts with one of '<'
, '>'
, '!'
or '='
. When using native size, the size of the packed value is platform-dependent.
Format |
C Type |
Python type |
Standard size |
Notes |
---|---|---|---|---|
|
pad byte |
no value |
||
|
|
bytes of length 1 |
1 |
|
|
|
integer |
1 |
(1), (2) |
|
|
integer |
1 |
(2) |
|
|
bool |
1 |
(1) |
|
|
integer |
2 |
(2) |
|
|
integer |
2 |
(2) |
|
|
integer |
4 |
(2) |
|
|
integer |
4 |
(2) |
|
|
integer |
4 |
(2) |
|
|
integer |
4 |
(2) |
|
|
integer |
8 |
(2) |
|
|
integer |
8 |
(2) |
|
|
integer |
(3) |
|
|
|
integer |
(3) |
|
|
(6) |
float |
2 |
(4) |
|
|
float |
4 |
(4) |
|
|
float |
8 |
(4) |
|
|
bytes |
||
|
|
bytes |
||
|
|
integer |
(5) |
Changed in version 3.3: Added support for the 'n'
and 'N'
formats.
Changed in version 3.6: Added support for the 'e'
format.
Notes:
-
The
'?'
conversion code corresponds to the_Bool
type defined by C99. If this type is not available, it is simulated using achar
. In standard mode, it is always represented by one byte. -
When attempting to pack a non-integer using any of the integer conversion codes, if the non-integer has a
__index__()
method then that method is called to convert the argument to an integer before packing.Changed in version 3.2: Use of the
__index__()
method for non-integers is new in 3.2. -
The
'n'
and'N'
conversion codes are only available for the native size (selected as the default or with the'@'
byte order character). For the standard size, you can use whichever of the other integer formats fits your application. -
For the
'f'
,'d'
and'e'
conversion codes, the packed representation uses the IEEE 754 binary32, binary64 or binary16 format (for'f'
,'d'
or'e'
respectively), regardless of the floating-point format used by the platform. -
The
'P'
format character is only available for the native byte ordering (selected as the default or with the'@'
byte order character). The byte order character'='
chooses to use little- or big-endian ordering based on the host system. The struct module does not interpret this as native ordering, so the'P'
format is not available. -
The IEEE 754 binary16 “half precision” type was introduced in the 2008 revision of the IEEE 754 standard. It has a sign bit, a 5-bit exponent and 11-bit precision (with 10 bits explicitly stored), and can represent numbers between approximately
6.1e-05
and6.5e+04
at full precision. This type is not widely supported by C compilers: on a typical machine, an unsigned short can be used for storage, but not for math operations. See the Wikipedia page on the half-precision floating-point format for more information.
data=struct.pack('9si2s',b'HTTP/1.1 ',200,b'OK')
9s代表9个字符,i代表是个整形,2s又是两个字符,后面的数据和前面的格式是对应的
REF:
https://www.kutu66.com//Python-Module-Examples/article_60350
https://www.zhaokeli.com/article/8000.html
https://blog.csdn.net/kingfoulin/article/details/81311416
https://docs.python.org/3/library/struct.html#format-characters