python3 抓包 解包
Python struct module is capable of performing the conversions between the Python values and C structs, which are represented as Python Strings.
Python struct 模块能够执行Python值和C结构之间的转换,这些值用Python Strings表示。
Python结构 (Python Struct)
- Python struct module can be used in handling binary data stored in files, database or from network connections etc. Python struct模块可用于处理存储在文件,数据库或网络连接等中的二进制数据。
- It uses format Strings as compact descriptions of the layout of the C structs and the intended conversion to/from Python values. 它使用格式字符串作为C结构布局的紧凑描述以及与Python值之间的预期转换。
Python结构函数 (Python Struct Functions)
There are five important functions in struct module – pack()
, unpack()
, calcsize()
, pack_into()
and unpack_from()
. In all these functions, we have to provide the format of the data to be converted into binary.
有在结构模块五个重要功能- pack()
unpack()
calcsize()
pack_into()
和unpack_from()
在所有这些功能中,我们必须提供要转换为二进制数据的格式。
Some of the popular format characters are:
一些流行的格式字符是:
?: boolean
h: short
l: long
i: int
f: float
q: long long int
You can get the complete list of format characters here. Let’s start looking into struct module functions one by one.
您可以在此处获取格式字符的完整列表。 让我们开始逐一研究struct模块的功能。
Python struct.pack() (Python struct.pack())
This function packs a list of values into a String representation of the specified type. The arguments must match the values required by the format exactly. Let’s quickly look at struct pack() example:
此函数将值列表打包为指定类型的String表示形式。 参数必须与格式所需的值完全匹配。 让我们快速看一下struct pack()示例:
import struct
var = struct.pack('hhl', 5, 10, 15)
print(var)
var = struct.pack('iii', 10, 20, 30)
print(var)
When we run this script, we get the following representation:
运行此脚本时,将获得以下表示形式:
Note that ‘b’ in the Output stands for binary.
请注意,输出中的“ b”代表二进制。
Python struct.unpack() (Python struct.unpack())
This function unpacks the packed value into its original representation with the specified format. This function always returns a tuple, even if there is only one element. Let’s quickly look at struct unpack() function example:
此函数将打包的值解压缩为指定格式的原始表示。 即使只有一个元素,此函数也总是返回一个元组 。 让我们快速看一下struct unpack()函数示例:
import struct
var = struct.pack('hhl', 5, 10, 15)
print(var)
print(struct.unpack('hhl', var))
When we run this script, we get back our original representation:
Clearly, we must tell the Python interpreter the format we need to unpack the values into.
运行此脚本时,我们将获得原始表示形式:
显然,我们必须告诉Python解释器将值解包所需的格式。
Python结构calcsize() (Python struct calcsize())
This function calculates and returns the size of the String representation of struct with a given format.
此函数以给定格式计算并返回struct的String表示形式的大小。
Size is calculated in terms of bytes. Let’s quickly look at an example code snippet:
大小以字节为单位计算。 让我们快速看一下示例代码片段:
import struct
var = struct.pack('hhl', 5, 10, 15)
print(var)
print("Size of String representation is {}.".format(struct.calcsize('hhl')))
When we run this script, we get the following representation:
运行此脚本时,将获得以下表示形式:
Python struct pack_into(),unpack_from() (Python struct pack_into(), unpack_from())
These functions allow us to pack the values into string buffer and unpack from a string buffer. These functions are introduced in version 2.5.
这些函数使我们可以将值打包到字符串缓冲区中,并从字符串缓冲区中解包。 这些功能在2.5版中引入。
import struct
# ctypes is imported to create a string buffer
import ctypes
# As shown in previous example
size = struct.calcsize('hhl')
print(size)
# Buffer 'buff' is created from ctypes
buff = ctypes.create_string_buffer(siz)
# struct.pack_into() packs data into buff and it doesn't return any value
# struct.unpack_from() unpacks data from buff, returns a tuple of values
print(struct.pack_into('hhl', buff, 0, 5, 10, 15))
print(struct.unpack_from('hhl', buff, 0))
When we run this script, we get the following representation:
运行此脚本时,将获得以下表示形式:
That’s all for a short introduction of python struct
module.
这就是对python struct
模块的简短介绍。
翻译自: https://www.journaldev.com/17401/python-struct-pack-unpack
python3 抓包 解包