def windows_shell(chan): # set signal somehow import threading stdout.write("*** Emulating terminal on Windows; press F6 or Ctrl+Z then " "enter to send EOF,\r\nor at the end of the execution.\r\n") stdout.flush() out_lock = threading.RLock() def write(recv, std): while True: data = recv(256) if not data: if std: with out_lock: stdout.write( "\r\n*** EOF reached; (press F6 or ^Z then enter " "to end)\r\n") stdout.flush() break stream = [stderr_bytes, stdout_bytes][std] with out_lock: stream.write(data) stream.flush() threading.Thread(target=write, args=(chan.recv, True)).start() threading.Thread(target=write, args=( chan.recv_stderr, False, )).start() try: while True: d = stdin_bytes.read(1) if not d: chan.shutdown_write() break try: chan.send(d) except socket.error: break except EOFError: # user hit ^Z or F6 pass
def windows_shell(chan): # set signal somehow import threading stdout.write("*** Emulating terminal on Windows; press F6 or Ctrl+Z then " "enter to send EOF,\r\nor at the end of the execution.\r\n") stdout.flush() out_lock = threading.RLock() def write(recv, std): while True: data = recv(256) if not data: if std: with out_lock: stdout.write( "\r\n*** EOF reached; (press F6 or ^Z then enter " "to end)\r\n") stdout.flush() break stream = [stderr_bytes, stdout_bytes][std] with out_lock: stream.write(data) stream.flush() threading.Thread(target=write, args=(chan.recv, True)).start() threading.Thread(target=write, args=(chan.recv_stderr, False,)).start() try: while True: d = stdin_bytes.read(1) if not d: chan.shutdown_write() break try: chan.send(d) except socket.error: break except EOFError: # user hit ^Z or F6 pass
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)