作业_day25
第一题
tcp三次握手与四次挥手
第二题
基于TCP开发一款远程CMD程序
客户端连接服务器后,可以向服务器发送命令
服务器收到命令后执行,无论执行是否成功,无论执行几遍,都将执行结果返回给客户端
# server.py
import socket
import subprocess
import time
server = socket.socket()
server.bind(('127.0.0.1',10086))
server.listen(3)
conn,addr = server.accept()
print(addr)
while True:
try:
data = conn.recv(1024).decode('utf8')
print(f'from client{addr}:{data}')
a = subprocess.getoutput(data)
if a:
conn.send(a.encode('utf-8'))
else:
conn.send('success'.encode('utf-8'))
except Exception as e:
print(e)
time.sleep(0.01)
# client.py
import socket
client = socket.socket()
client.connect(('127.0.0.1',10086))
while True:
send_msg = input('发送消息:')
client.send(send_msg.encode('utf-8'))
data = client.recv(1024).decode('utf-8')
print(f'from server:{data}')