编写远程执行命令的CS架构软件
客户端
import socket
cmd_user = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
cmd_user.connect(('127.0.0.1',8080))
while True:
cmd = input('cmd:>>').strip()
if len(cmd) == 0:
continue
cmd_user.send(cmd.encode('utf-8'))
respond = cmd_user.recv(1024)
print(respond.decode('utf-8'))
cmd_user.close()
服务端
import socket
cmd_user = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
cmd_user.bind(('127.0.0.1',8080))
cmd_user.listen(5)
while True:
conn,client_addr = cmd_user.accept()
while True:
try:
cmd = conn.recv(2048)
if len(cmd) == 0:
break
exec(cmd)
print(执行成功!')
conn.send(f'{cmd}执行成功'.encode('utf-8'))
except Exception:
break
conn.close()
cmd_user.close()