python tcp以及数据分包(struct)
import json
import struct
import cv2
ver = 1
body = json.dumps(dict(hello=“world”))
print(body)
cmd = 101
header = [ver, body.len(), cmd]
print(type(body.len()))
print(body.len())
默认低位在前,高位在后,“<”模式
b = struct.pack(‘ihb’, 1, 2, 3)
pack后就变成了C结构的二进制串,转成 python的string类型来显示
C type:i int h short b signedchar
i 代表C struct中的int类型,故而本机占4位,1则表示为01000000;
h 代表C struct中的short类型,占2位,故表示为0200;
同理b 代表C struct中的signed char类型,占1位,故而表示为03。
s = struct.unpack(‘ihb’, b)
print(“")
print(b)
print(s)
print("”)
data = [1, 2, 3]
网络结构 :“!”和“>”相同,高位在前
buffer = struct.pack(’!ihb’, data)
buffer_u = struct.unpack(’!ihb’, buffer)
print(buffer)
print(buffer_u)
print("******************")
压缩解压不同模式,则解压错误
buffer_s = struct.unpack(‘ihb’, buffer)
print(buffer_s)
这样,可以让数据按自己规定的模式进行传输
tcp分包、粘包处理传输示例:
客户端:
import socket
import time
import struct
import json
host = “localhost”
port = 1234
ADDR = (host, port)
if name == ‘main’:
client = socket.socket()
client.connect(ADDR)
正常数据包定义
ver = 1
body = json.dumps(dict(hello=“world”))
print(body)
cmd = 101
header = [ver, body.len(), cmd]
print(type(body.len()))
print(body.len())
headPack = struct.pack("!3I", *header)
sendData1 = headPack+body.encode()
分包数据定义
ver = 2
body = json.dumps(dict(hello=“world2”))
print(body)
cmd = 102
header = [ver, body.len(), cmd]
headPack = struct.pack("!3I", *header)
sendData2_1 = headPack+body[:2].encode()
sendData2_2 = body[2:].encode()
粘包数据定义
ver = 3
body1 = json.dumps(dict(hello=“world3”))
print(body1)
cmd = 103
header = [ver, body1.len(), cmd]
headPack1 = struct.pack("!3I", *header)
ver = 4
body2 = json.dumps(dict(hello=“world4”))
print(body2)
cmd = 104
header = [ver, body2.len(), cmd]
headPack2 = struct.pack("!3I", *header)
sendData3 = headPack1+body1.encode()+headPack2+body2.encode()
正常数据包
client.send(sendData1)
time.sleep(3)
分包测试
client.send(sendData2_1)
time.sleep(0.2)
client.send(sendData2_2)
time.sleep(3)
粘包测试
client.send(sendData3)
time.sleep(3)
client.close()
服务端:
Python Version:3.5.1
import socket
import struct
HOST = ‘’
PORT = 1234
dataBuffer = bytes()
headerSize = 12
sn = 0
def dataHandle(headPack, body):
global sn
sn += 1
print(“第%s个数据包” % sn)
print(“ver:%s, bodySize:%s, cmd:%s” % headPack)
print(body.decode())
print("")
if name == ‘main’:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((HOST, PORT))
s.listen(1)
conn, addr = s.accept()
with conn:
print(‘Connected by’, addr)
while True:
data = conn.recv(1024)
if data:
把数据存入缓冲区,类似于push数据
dataBuffer += data
while True:
if len(dataBuffer) < headerSize:
print(“数据包(%s Byte)小于消息头部长度,跳出小循环” % len(dataBuffer))
break
读取包头
struct中:!代表Network order,3I代表3个unsigned int数据
headPack = struct.unpack(’!3I’, dataBuffer[:headerSize])
bodySize = headPack[1]
分包情况处理,跳出函数继续接收数据
if len(dataBuffer) < headerSize+bodySize :
print(“数据包(%s Byte)不完整(总共%s Byte),跳出小循环” % (len(dataBuffer), headerSize+bodySize))
break
读取消息正文的内容
body = dataBuffer[headerSize:headerSize+bodySize]
数据处理
dataHandle(headPack, body)
粘包情况的处理
dataBuffer = dataBuffer[headerSize+bodySize:] # 获取下一个数据包,类似于把数据pop出