Python: struct.pack_into 和 struct.unpack_from 函数学习

本文介绍了Python中的struct.pack_into和struct.unpack_from函数,用于处理自定义协议的打包和解包。这两个函数允许在缓冲区的特定偏移位置进行操作,适合接口测试中涉及二进制数据的场景。通过示例和应用场景阐述了它们在自定义协议接口测试中的重要作用。
摘要由CSDN通过智能技术生成

一、 函数定义

 

struct.pack_into(fmt, buffer, offset, v1, v2, ...)

Pack the values v1, v2, ... according to the format string fmt and write the packed bytes into the writable buffer buffer starting at position offset. Note that offset is a required argument.

按照指定的格式fmt,将v1,v2...打包到buffer中,其中偏移位置为offset

 

struct.unpack_from(fmt, buffer, offset=0)

Unpack from buffer starting at position offset, according to the format string fmt. The result is a tuple even if it contains exactly one item. The buffer’s size in bytes, minus offset, must be at least the size required by the format, as reflected by calcsize().

按照指定的格式fmt,从偏移位置offset开始解包,返回数据格式是一个元组(v1,v2...)

 

二、简单的脚本示例

 

from ctypes import create_string_buffer
import struct
import binascii

#创建了一个7个字节长度的buffe
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值