服务端: ``` import socket import subprocess phone=socket.socket(socket.AF_INET,socket.SOCK_STREAM) # 流式协议=》tcp协议 phone.bind(('127.0.0.1',8080)) # 0-65535, 1024以前的都被系统保留使用 phone.listen(5) # 5指的是半连接池的大小 conn,client_addr=phone.accept() while True: try: data = conn.recv(1024) # 最大接收的数据量为1024Bytes,收到的是bytes类型 if data == 'q': break print("客户端发来的消息:", data.decode('gbk')) obj = subprocess.Popen(data.decode('gbk'), shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, ) res = obj.stdout.read() err_res = obj.stderr.read() # if not res: # break conn.send(res) # if not err_res: # break conn.send(err_res) except Exception: print('输入有误') break conn.close() ``` 客户端 ``` import socket phone=socket.socket(socket.AF_INET,socket.SOCK_STREAM) # 流式协议=》tcp协议 phone.connect(('127.0.0.1',8080)) while True: msg=input("输入要发送的消息>>>: ").strip() if len(msg) == 0:continue phone.send(msg.encode('gbk')) data=phone.recv(1024) print(data.decode('gbk')) phone.close() ```