一. subprocess.Popen
客户端传输一个输入的命令给服务器端
服务器收到后执行此命令并且将结果返回给客户端
客户端收到命令执行结果后呈现到用户眼前
#server服务器端 import socket import subprocess sk = socket.socket() sk.bind(('127.0.0.1',8080)) sk.listen() conn,addr = sk.accept() while 1: cmd = conn.recv(1024).decode('utf-8') r = subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE) stdout = r.stdout.read() stderr = r.stderr.read() if stderr: conn.send(stderr)#错误结果或不可执行的命令 else: conn.send(stdout)#正确结果 conn.close() sk.close()
#client客户端 import socket sk = socket.socket() sk.connect_ex(('127.0.0.1',8080)) while 1: cmd = input('请输入一个命令>>>') sk.send(cmd.encode('utf-8')) result = sk.recv(102400).decode('gbk') print(result) sk.close()