python 之解决socket 粘包

1、low版本(用户端):

from socket import *
ip_port = ("127.0.0.1",8080)
back_log = 5
buffer_size = 1024
tcp_client = socket(AF_INET,SOCK_STREAM)  #IPv4 Internet协议,SOCK_STREAM字节流,对应TCP,SOCK_DGRAM数据报文,对应UDP
tcp_client.connect(ip_port)
while 1:
    cmd = input(">>>>>>>>:").strip()
    if not cmd:
        continue
    if cmd == "quit":
        break
    tcp_client.send(cmd.encode("utf-8"))
    #解决粘包

    length = tcp_client.recv(buffer_size)
    tcp_client.send(b"ready")

    length = int(length.decode("utf-8"))
    recv_size = 0
    recv_msg = b""
    while recv_size < length:
        cmd_res = tcp_client.recv(buffer_size)
        recv_msg = recv_msg + cmd_res
        recv_size = len(recv_msg)
    print("命令的执行结果是",recv_msg.decode("gbk"))
tcp_client.close()

服务端:

from socket import *
import  subprocess
ip_port = ("127.0.0.1",8080)
back_log = 5
buffer_size = 1024
tcp_server = socket(AF_INET,SOCK_STREAM)
tcp_server.bind(ip_port)
tcp_server.listen(back_log)
while 1:
    conn,adr = tcp_server.accept()
    print("新的客户端连接是%s,地址是%s"%(conn,adr))
    while 1:
        try:
            cmd = conn.recv(buffer_size)
            if not cmd:
                break
            print("收到客户端的命令",cmd)
            res = subprocess.Popen(cmd.decode("utf-8"),shell= True,stdout=subprocess.PIPE,stderr=subprocess.PIPE,stdin=subprocess.PIPE)
            err = res.stderr.read()
            # if not err:#为空
            if err:#有值
                cmd = err
            else:
                cmd_res = res.stdout.read()#bite

            if not cmd_res:
                cmd_res = "执行成功".encode("gbk")

            length = len(cmd_res)

            conn.send(str(length).encode("utf-8"))


            client_ready = conn.recv(buffer_size)

            if client_ready == b"ready":

                conn.send(cmd_res)
        except Exception as e:
            print(e)
            break
    conn.close()

2、高逼格版本:(提升性能)

用户端:

from socket import *
import  struct
ip_port = ("127.0.0.1",8080)
back_log = 5
buffer_size = 1024
tcp_client = socket(AF_INET,SOCK_STREAM)
tcp_client.connect(ip_port)
while 1:
    cmd = input(">>>>>>>>:").strip()
    if not cmd:
        continue
    if cmd == "quit":
        break
    tcp_client.send(cmd.encode("utf-8"))
    #解决粘包
    length_data = tcp_client.recv(4)
    length = struct.unpack("i",length_data)[0]
    recv_size = 0
    recv_msg = b""
    while recv_size < length:
        cmd_res = tcp_client.recv(buffer_size)
        recv_msg = recv_msg + cmd_res
        recv_size = len(recv_msg)
    print("命令的执行结果是",recv_msg.decode("gbk"))
tcp_client.close()

 服务端:

from socket import *
import  subprocess
import struct
ip_port = ("127.0.0.1",8080)
back_log = 5
buffer_size = 1024
tcp_server = socket(AF_INET,SOCK_STREAM)
tcp_server.bind(ip_port)
tcp_server.listen(back_log)
while 1:
    conn,adr = tcp_server.accept()
    print("新的客户端连接是%s,地址是%s"%(conn,adr))
    while 1:
        try:
            cmd = conn.recv(buffer_size)
            if not cmd:
                break
            print("收到客户端的命令",cmd)
            res = subprocess.Popen(cmd.decode("utf-8"),shell= True,stdout=subprocess.PIPE,stderr=subprocess.PIPE,stdin=subprocess.PIPE)
            err = res.stderr.read()
            # if not err:#为空
            if err:#有值
                cmd = err
            else:
                cmd_res = res.stdout.read()#bite
            if not cmd_res:
                cmd_res = "执行成功".encode("gbk")
            length = len(cmd_res)
            data_length = struct.pack("i",length)
            conn.send(data_length)
            conn.send(cmd_res)
        except Exception as e:
            print(e)
            break
    conn.close()

 

posted @ 2018-12-15 16:30  高母寨寨委书记  阅读(252)  评论(0编辑  收藏  举报