一、i类型的字节数限制,如下代码报错:
1 import struct 2 3 4 # res = struct.pack('i', 1230) 5 # print(res, type(res), len(res)) 6 # 7 # obj = struct.unpack('i', res) 8 # print(obj[0]) 9 10 res = struct.pack('i', 1233444444555555) 11 12 报错结果为: 13 14 Traceback (most recent call last): 15 File "C:/Users/xu516/PycharmProjects/Python全栈开发/第三模块/网络编程/05 解决粘包问题/简单版/struct模块的使用.py", line 10, in <module> 16 res = struct.pack('i', 1233444444555555) 17 struct.error: argument out of range
二、l类型也有字节数限制,如下代码报错:
1 import struct 2 3 4 # res = struct.pack('i', 1230) 5 # print(res, type(res), len(res)) 6 # 7 # obj = struct.unpack('i', res) 8 # print(obj[0]) 9 10 # res = struct.pack('i', 1233444444555555) 11 12 res = struct.pack('l', 123344444455555556789012345678901234567890123456789) 13 print(res, len(res)) 14 15 报错结果为: 16 17 Traceback (most recent call last): 18 File "C:/Users/xu516/PycharmProjects/Python全栈开发/第三模块/网络编程/05 解决粘包问题/简单版/struct模块的使用.py", line 12, in <module> 19 res = struct.pack('l', 123344444455555556789012345678901234567890123456789) 20 struct.error: argument out of range
三、可以用字典存储数据,
如:
1 header_dic = { 2 'filename': 'a.txt', 3 'md5': 'xxdxx', 4 'total_size': '134567879809876545236557687958765432567897654365785432457865433336754' 5 } 6 7 8 header_json = json.dumps(header_dic) 9 print(type(header_json)) 10 11 12 结果为: 13 14 <class 'str'>
str就可以转成bytes类型发送出去了,
1 header_dic = { 2 'filename': 'a.txt', 3 'md5': 'xxdxx', 4 'total_size': '134567879809876545236557687958765432567897654365785432457865433336754' 5 } 6 7 8 header_json = json.dumps(header_dic) 9 # print(type(header_json)) 10 header_bytes = header_json.encode('utf-8') 11 print(type(header_bytes)) 12 13 结果为: 14 15 <class 'bytes'>