1.整理TCP三次握手、四次挥手图
2.基于TCP开发一款远程CMD程序
客户端连接服务器后,可以向服务器发送命令
服务器收到命令后执行,无论执行是否成功,无论执行几遍,都将执行结果返回给客户端
注意: 执行系统指令使用subprocess模块完成.
# 服务端.py import socket import subprocess sever = socket.socket() sever.bind(('127.0.0.1',1111)) sever.listen(5) while True: conn,addr = sever.accept() while True: try: data = conn.recv(1024).decode('utf8') if data =='q': break res = subprocess.Popen(data,shell=True,stderr=subprocess.PIPE,stdout=subprocess.PIPE) data1 = res.stdout.read() data2 = res.stderr.read() conn.send(data1+data2) except Exception as e: print(e) conn.close()
# 客户端,py import socket client = socket.socket() client.connect(('127.0.0.1',1111)) while True: msg = input('>>>') client.send(msg.encode('utf8')) if msg == 'q': break data = client.recv(1024).decode('gbk') print(data) client.close()
来源:https://www.cnblogs.com/wick2019/p/11695240.html