(endpoint)
| 42 | |
| 43 | |
| 44 | def invoke_shell(endpoint): |
| 45 | ssh = websocket.create_connection(endpoint) |
| 46 | _resize(ssh) |
| 47 | oldtty = termios.tcgetattr(sys.stdin) |
| 48 | old_handler = signal.getsignal(signal.SIGWINCH) |
| 49 | |
| 50 | def on_term_resize(signum, frame): |
| 51 | _resize(ssh) |
| 52 | signal.signal(signal.SIGWINCH, on_term_resize) |
| 53 | |
| 54 | try: |
| 55 | tty.setraw(sys.stdin.fileno()) |
| 56 | tty.setcbreak(sys.stdin.fileno()) |
| 57 | |
| 58 | rows, cols = _pty_size() |
| 59 | ssh.send(json.dumps({'resize': {'width': cols, 'height': rows}})) |
| 60 | |
| 61 | while True: |
| 62 | try: |
| 63 | r, w, e = select.select([ssh.sock, sys.stdin], [], []) |
| 64 | if ssh.sock in r: |
| 65 | data = ssh.recv() |
| 66 | if not data: |
| 67 | break |
| 68 | message = json.loads(data) |
| 69 | if 'error' in message: |
| 70 | raise ConnectionError(message['error']) |
| 71 | sys.stdout.write(message['data']) |
| 72 | sys.stdout.flush() |
| 73 | if sys.stdin in r: |
| 74 | x = sys.stdin.read(1) |
| 75 | if len(x) == 0: |
| 76 | break |
| 77 | ssh.send(json.dumps({'data': x})) |
| 78 | except (select.error, IOError) as e: |
| 79 | if e.args and e.args[0] == errno.EINTR: |
| 80 | pass |
| 81 | else: |
| 82 | raise |
| 83 | except websocket.WebSocketException: |
| 84 | raise |
| 85 | finally: |
| 86 | termios.tcsetattr(sys.stdin, termios.TCSADRAIN, oldtty) |
| 87 | signal.signal(signal.SIGWINCH, old_handler) |
nothing calls this directly
no test coverage detected