远程执行命令
先来学习一个新模块 , 一会用到的..
新模块: subprocess 执行系统命令 r = subprocess.Popen('ls',shell=True,stdout=subprocess.PIPE, stderr=subprocess.PIPE) subprocess.Popen(a,b,c,d) a: 要执行的系统命令(str) b: shell = True 表示确定我当前执行的命令为系统命令 c: 表示正确信息的输出管道 d: 表示错误信息的输出管道
下边直接上代码,一看就懂. TCP的
import socket import subprocess sk = socket.socket() sk.bind(('127.0.0.1',9090)) sk.listen() conn,addr = sk.accept() while 1: cmd = conn.recv(1024).decode('utf-8') res = subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE, stderr=subprocess.PIPE) std_out = res.stdout.read()# 读取正确的返回信息 std_err = res.stderr.read()# 读取错误的返回信息 if std_out: conn.send(std_out) else: conn.send(std_err) conn.close() sk.close()