def __init__(self, cmd): self.popen = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) pid = self.popen.pid sysout.info(TAG, 'Popen.pid:' + str(pid))
def invoke_shell(self, cmd): sysout.info(TAG, "execute cmd: '" + cmd + "'") channel = self.client.invoke_shell() readable, writeable, error = select.select([ channel, sys.stdin, ], [], [], 1) # while not channel.recv_ready(): # sysout.info(TAG, "invoke is running...") buff = channel.recv(1024) x = u(buff) sys.stdout.write(x) sys.stdout.flush() # base_prompt = r'(>|#|\]|\$|\)) *$' # while(not re.search(base_prompt, buff.split('\n')[-1])): # _buff = channel.recv(2048) # buff += _buff # print(buff) channel.send(cmd) if sys.stdin in readable: inp = sys.stdin.readline() print("inp: " + inp) channel.sendall(inp)
def application(environ, start_response): # 定义请求的类型和当前请求成功的code start_response('200 OK', [('Content-Type', 'application/json')]) # environ是当前请求的所有数据,包括Header和URL,body request_body = environ["wsgi.input"].read( int(environ.get("CONTENT_LENGTH", 0))).decode('utf-8') sysout.info(TAG, 'request: ' + str(request_body)) # request_body = json.loads(request_body) response = praseData(request_body) return [json.dumps(response)]
def __init__(self, host, port: int, usr, pwd, keyFile=None, keyPwd=None): global isClosed sysout.info(TAG, "ssh to server... [" + host + ":" + str(port) + "]") self.client = paramiko.SSHClient() # key = paramiko.RSAKey.from_private_key_file(keyFile, password=keyPwd) self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy( )) # 自动接受密钥, 通过公共方式进行认证 (不需要在known_hosts 文件中存在) self.client.connect(host, port, username=usr, password=pwd) isClosed = True sysout.info(TAG, "ssh success")
def run(): httpd = HTTPServer(mServer, NotebookHTTPServer) # httpd = make_server(mHost, mPort, application) sysout.info(TAG, 'http server is running on ' + str(mServer)) # httpd = NotebookHTTPServer(mServer) httpd.serve_forever()
def close(self): global isConnecting sysout.info(TAG, "ssh closed.") self.client.close() isConnecting = False
def executeCmd(self, cmd): sysout.info(TAG, "execute cmd: '" + cmd + "'") return self.client.exec_command(cmd)