编写远程执行命令的CS架构软件
服务器
import socket msg = socket.socket(socket.AF_INET, socket.SOCK_STREAM) msg.bind(('127.0.0.1', 7242)) msg.listen(5) while 1: conn, addr = msg.accept() while 1: try: data = conn.recv(1024) if len(data) == 0: break print('客户端的信息:', data.decode('utf-8')) conn.send(data.upper()) except Exception: break conn.close()
客户端
import socket msg = socket.socket(socket.AF_INET, socket.SOCK_STREAM) msg.connect(('127.0.0.1', 7242)) while 1: res = input('请输入信息:').strip() if len(res) == 0: continue msg.send(res.encode('utf-8')) data = msg.recv(1024) print(data.decode('utf-8')) msg.close()