python3 抓包 解包_Python结构包,解包

本文介绍了Python3的struct模块,用于处理二进制数据。重点讲解了struct.pack()、struct.unpack()、struct.calcsize()以及struct.pack_into()、unpack_from()函数的使用,包括格式字符及其在处理网络、文件或数据库中二进制数据时的应用。
摘要由CSDN通过智能技术生成

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:

python struct pack

运行此脚本时,将获得以下表示形式:

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:

python struct unpack

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 calcsize

运行此脚本时,将获得以下表示形式:

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:

Python Struct pack_into and unpack_from string buffer

运行此脚本时,将获得以下表示形式:

That’s all for a short introduction of python struct module.

这就是对python struct模块的简短介绍。

翻译自: https://www.journaldev.com/17401/python-struct-pack-unpack

python3 抓包 解包

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值