作业25
编写远程执行命令的CS架构软件
服务端
# 服务端
import socket
import subprocess
handle = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
handle.bind(('127.0.0.1', 8080))
handle.listen(5)
while 1:
conn, client_adr = handle.accept()
while 1:
try:
data = conn.recv(1024)
data = data.decode("utf-8")
print(client_adr)
# data = data.decode("utf-8")
obj = subprocess.Popen(data, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
stdin=subprocess.PIPE)
res1 = obj.stdout.read()
if len(res1) == 0:
conn.send("执行成功".encode("utf-8"))
conn.send(res1)
except Exception:
break
conn.close()
handle.close()
客户端
import socket
handle = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
handle.connect(('127.0.0.1', 8080))
while 1:
cmd = input("输入命令").strip()
if len(cmd) == 0:
break
handle.send(cmd.encode("utf-8"))
res = handle.recv(1024)
print(res.decode("utf-8"))
handle.close()
# start C:\Users\wwh\Desktop\网盘.txt
1