python tcp以及数据分包(struct)

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出

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值