1. Arrange the TCP three-way handshake, four waved map
Three-way handshake
Four waving
2. Based on the development of a remote TCP CMD program
After the client connect to the server, the server can send commands to the
server to perform after receiving the command, whether or not executed successfully, regardless of execution several times, the results will be returned to the client
Note: an instruction execution system using subprocess module is completed.
Server
from socket import *
import subprocess
server=socket(AF_INET,SOCK_DGRAM)
server.bind(('127.0.0.1',9003))
while True:
#收消息
cmd,addr=server.recv(1024)
print('用户命令----->',cmd)
#逻辑处理
res=subprocess.Popen(cmd.decode('utf-8'),shell=True,stderr=subprocess.PIPE,stdin=subprocess.PIPE,stdout=subprocess.PIPE)
stderr=res.stderr.read()
stdout=res.stdout.read()
#发消息
server.sendto(stderr,addr)
server.sendto(stdout,addr)
server.close()
Client
from socket import *
client=socket(AF_INET,SOCK_DGRAM)
while True:
msg=input('>>: ').strip()
client.sendto(msg.encode('utf-8'),('127.0.0.1',9003))
data,addr=client.recv(1024)
print(data.decode('utf-8'),end='')