我想向服务器发送请求,但不知何故Python将4Byte值转换为8 byte值:
struct.pack("H", socket.ntohs(v1)) + struct.pack("?", v2) + struct.pack("?", v3) + struct.pack("L", socket.ntohl(v5)) + struct.pack("L", socket.ntohl(v6)) + struct.pack("L", socket.ntohl(v7)) + struct.pack("L", socket.ntohl(v8)) + struct.pack("H", socket.ntohs(v9))
高达v5一切工作正常,但v5之后,我总是得到8Byte值而不是4Bytes。Wireshark捕捉:Up 00 00 03 e8一切正常
0040 4d 65 01 00 00 00 01 00 00 01 01 00 00 03 e8 00 Me...... ........
0050 00 00 00 00 00 07 d0 00 00 00 00 00 00 0b b8 00 ........ ........
0060 00 00 00 00 00 0f a0 00 00 00 00 13 88 00 00 00 ........ ........
由于您没有指定其他方式,因此您使用的是long
的本机表示。尝试添加=
说明符(本机字节顺序,标准大小):
struct.pack("=H??LLLLH",
socket.ntohs(v1),
v2,
v3,
socket.ntohl(v5),
socket.ntohl(v6),
socket.ntohl(v7),
socket.ntohl(v8),
socket.ntohs(v9))
引用:
- http://docs.python.org/3/library/struct.html byte-order-size-and-alignment
- http://docs.python.org/3/library/struct.html格式字符