1
cqcsdzmt OP 另外,如果接收同样带包头的数据,怎么把包头单独掐出来挨个解析呢?
|
2
codechaser 2018 年 8 月 17 日 via Android
是要把这个包转换成 str 加到字符串前面?
|
3
ebingtel 2018 年 8 月 17 日
struct 试试?
|
4
anjianshi 2018 年 8 月 17 日 content = "this is udp client data\n"
flag = b'\xad\x0e\xfd\x23' hex_t = hex(len(content))[2:].zfill(4) len_hex = chr(int(hex_t[:2], 16)) + chr(int(hex_t[2:4], 16)) len_hex = len_hex.encode() type_ = b'\x00\x00\x00\x01' rest = b'\x00\x00\x00\x00' result = flag + len_hex + type_ + rest + content.encode() print(result) |
6
HelloAmadeus 2018 年 8 月 17 日 via Android
struct 模块了解一下,这是 Python2 吗,str 竟然可以直接发送
|
7
a132811 2018 年 8 月 17 日
用 Python3 bytes 就解决问题了。更复杂的转换,用 struct。https://ahuigo.github.io/#/post/py/py-str-struct.md
|
9
cqcsdzmt OP @HelloAmadeus 对,Python2
|
11
cqcsdzmt OP @codechaser 不一定非要转换成 str,只在发送时在前面加上包头就可以
|
12
a132811 2018 年 8 月 17 日
不是年久失修的老破烂项目,就装 python3 吧,又不困难。python2 这些恶心的字符问题,我才不想面对呢
|