liusarazhang 2019-10-20
原理:利用套字节发送命令,在服务端执行,并将执行的结果返回客户端。
在python3中发送文本使用:send()把字符串encode进行编码,接受recv()把字符串使用decode进行解码。
# coding:utf-8 import socket import os # 创建套字节 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # 绑定套字节(①获取主机名,②设置端口) host = socket.gethostname() port = 7777 s.bind((host, port)) # 监听 s.listen(socket.SOMAXCONN) print("等待连接......") # 处理连接(获取请求主机的参数) c, addr = s.accept() print("连接成功") c.send(‘连接成功‘.encode(‘utf-8‘)) while True: cmd=c.recv(1024).decode(‘utf-8‘) result=os.popen(cmd).read() c.send(str(result).encode(‘utf-8‘)) s.close()
远控服务端
# coding:utf-8 import socket c = socket.socket(socket.AF_INET, socket.SOCK_STREAM) host = socket.gethostname() port = 7777 c.connect((host, port)); msg = c.recv(1024).decode(‘utf-8‘) print(msg) while True: cmd = input("请输入命令:") c.send(cmd.encode(‘utf-8‘)) result=c.recv(1024).decode(‘utf-8‘) print(result) c.close()
远控客户端