作者:n大牙 | 来源:互联网 | 2023-10-12 13:19
客户端向服务器发送CMD指令,并返回执行结果.#服务器端importsocketimportsubprocess#subprocess模块可用于执行cmd命令sksocket
客户端向服务器发送CMD指令,并返回执行结果.
import socket
import subprocess sk = socket.socket()
sk.bind(('127.0.0.1',8080))
sk.listen()conn, addr = sk.accept()cmd = conn.recv(1024).decode('utf-8')r = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout = r.stdout.read()
stderr = r.stderr.read()if stderr: conn.send(stderr)
else:conn.send(stdout)conn.close()
sk.close()
import socketsk = socket.socket()sk.connect_ex(('127.0.0.1',8080))cmd = input('输入一个命令>>>')
sk.send(cmd.encode('utf-8'))result = sk.recv(102400).decode('gbk')
print(result)sk.close()