server端:
import socket import subprocess server=socket.socket() server.bind(('127.0.0.1',4444)) server.listen(5) while True: conn,addr=server.accept() print("new conn:",addr) while True: print(addr) data=conn.recv(1024) print(len(data)) print(data.decode()) cmd_rese=subprocess.Popen(data.decode(),shell=True,stdout=subprocess.PIPE) outread=cmd_rese.stdout.read() print(len(outread)) conn.send(("%s" % len(outread)).encode("utf-8")) print(outread.decode("gb2312")) conn.send(outread)
kehu端:
import socket client=socket.socket() client.connect(("127.0.0.1",4444)) while True: cmd=input(">>:").strip() if len(cmd)==0:continue print(cmd) client.send(cmd.encode("utf-8")) print("res") res_size=client.recv(1024).decode("utf-8") size=0 while size<int(res_size): res=client.recv(1024) print(res.decode("gb18030")) size+=len(res)
运行图: