Fork me on GitHub
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()
posted on 2020-01-03 17:55  OBOS  阅读(139)  评论(0编辑  收藏  举报