def call_output(args, stream='out'): if stream == 'out': kw = {'stdout': subprocess.PIPE} elif stream == 'err': kw = {'stderr': subprocess.PIPE} else: raise ValueError p = subprocess.Popen(args, **kw) if stream == 'out': fp = p.stdout else: fp = p.stderr output = [] while True: line = fp.readline() if not line: break stderr_bytes.write(line) output.append(line) retcode = p.wait() return retcode, b''.join(output)
def posix_shell(chan, raw): # set signal somehow import select oldtty = termios.tcgetattr(stdin_bytes) try: if raw: tty.setraw(stdin_bytes.fileno()) tty.setcbreak(stdin_bytes.fileno()) chan.settimeout(0.0) while True: r, w, e = select.select([chan, stdin_bytes], [], []) if chan in r: try: if chan.recv_stderr_ready(): x = chan.recv_stderr(1024) if len(x) > 0: stderr_bytes.write(x) stderr_bytes.flush() else: x = chan.recv(1024) if len(x) == 0: break stdout_bytes.write(x) stdout_bytes.flush() except socket.timeout: pass if stdin_bytes in r: x = stdin_bytes.read(1) if len(x) == 0: break chan.send(x) finally: if raw: termios.tcsetattr(stdin_bytes, termios.TCSADRAIN, oldtty)