Python 为什么';我的数据的压缩长度是否与结构中声明的长度相对应?
我正在开发一个bittorrent实现,以供自己借鉴,但在将字节打包到握手包中时遇到了一些问题。下表详细说明了我正在处理的数据的性质: 注意:握手信息遵循Python 为什么';我的数据的压缩长度是否与结构中声明的长度相对应?,python,struct,bittorrent,Python,Struct,Bittorrent,我正在开发一个bittorrent实现,以供自己借鉴,但在将字节打包到握手包中时遇到了一些问题。下表详细说明了我正在处理的数据的性质: 注意:握手信息遵循 “> 我已经验证了我所有的数据变量都具有预期的长度,但是我没有得到长度为68的压缩结构,而是得到长度为72的压缩结构。下面是一个测试用例: from struct import Struct handshake = Struct('B19sQ20s20s') pstrlen = 19 pstr = 'BitTorrent protocol
“>
我已经验证了我所有的数据变量都具有预期的长度,但是我没有得到长度为68的压缩结构,而是得到长度为72的压缩结构。下面是一个测试用例:
from struct import Struct
handshake = Struct('B19sQ20s20s')
pstrlen = 19
pstr = 'BitTorrent protocol'
reserved = 0
info_hash = 'x' * 20
peer_id = 'y' * 20
pkg = handshake.pack(pstrlen, pstr, reserved, info_hash, peer_id)
print len(pkg)
我显然遗漏了一些明显的东西。给出了什么?注意
struct.calcsize('b19sq20s'
)返回72;struct.calcsize('tryhandshake=struct('b19sq20s')
@JoranBeasley,使事情变得更大(
)似乎是玩了这个把戏,但我不知道为什么。B
是未签名的,那么endian ness在哪里发挥作用呢?无论如何,谢谢!
from struct import Struct
handshake = Struct('B19s8B20s20s')
pstrlen = 19
pstr = 'BitTorrent protocol'
info_hash = 'x' * 20
peer_id = 'y' * 20
pkg = handshake.pack(pstrlen, pstr, 0,0,0,0,0,0,0,0, info_hash, peer_id)
print len(pkg)