Python学习之sockets实现ssh解决粘包问题
struct模块,将数据长度封装为一个统一长度的报头,首先进行发送。另一端进行recv时,先对报头进行解码,再以解码后的数字,对剩下的数据进行解码。
import socket import os,time,struct server = socket.socket() server.bind(('localhost',9999)) #server.bind(('0.0.0.0',9999)) server.listen() while True: conn,addr= server.accept() print("new conn",addr) while True: print("等待新指令") data= conn.recv(1024) if not data: print("客户端已断开") break print("执行指令:",data) cmd_res = os.popen(data.decode()).read() # 接收字符串,执行结果也是字符串 print("before send",len(cmd_res)) if len(cmd_res) == 0: cmd_res = "cmd has no output" msg_size = len(cmd_res.encode()) size = struct.pack('i',msg_size) conn.send(size+cmd_res.encode("utf-8")) print("send done") server.close()
#!/usr/bin/env python #-*-coding:utf8-*- import socket,struct client = socket.socket() client.connect(('localhost',9999)) while True: cmd = input(">>:").strip() if len(cmd) == 0: continue client.send(cmd.encode("utf-8")) cmd_res_size = client.recv(4) #接受命令结果的长度,命令的大小 msg_size = struct.unpack('i',cmd_res_size)[0] print("命令结果大小:", msg_size) received_size= 0 #收到的大小 ,bytes类型 received_data = b'' while received_size < msg_size: data = client.recv(1024) received_size += len(data) #每次收到的有可能小于1024,所以必须用len判断 received_data += data else: print("cmd res receive done...",received_size) print(received_data.decode()) client.close()
粘包问题,服务器端发送大小和执行结果给客户端时,把2个send操作的内容当作一次,一起传到了客户端,造成错误
即多类数据组合成一个数据块。如:1,‘vz’,9.9 ——> 1aaa9.9 应该分成三次发送的
struct模块,将数据长度封装为一个统一长度的报头,首先进行发送。另一端进行recv时,先对报头进行解码,再以解码后的数字,对剩下的数据进行解码。