解决粘包问题2服务端

from socket import *
import subprocess,struct,json
server=socket(AF_INET,SOCK_STREAM)
server.bind(('127.2.2.2',6688))
server.listen(5)
while True:
    conn,client_addr=server.accept()
    print(client_addr)
    while True:
        try:
           cmd=conn.recv(1024)
           if not cmd:break
           obj=subprocess.Popen(cmd.decode('utf-8'),shell=True,
                                stdout=subprocess.PIPE,
                                stderr=subprocess.PIPE
                                )
           stdout=obj.stdout.read()
           stderr=obj.stderr.read()
           headers={'filepath':'a.txt',
                    'md5':'123fvs54',
                    'total_size':len(stdout)+len(stderr)}
           headers_json=json.dumps(headers)
           headers_bytes=headers_json.encode('utf-8')
           conn.send(struct.pack('i',len(headers_bytes)))
           conn.send(headers_bytes)
           conn.send(stdout)
           conn.send(stderr)
        except ConnectionResetError:break
    conn.close()
server.close()
上一篇:系统相关的信息模块——sys


下一篇:curl 基本用法