|
| 1 | +#!/usr/local/bin/python |
| 2 | + |
| 3 | +import argparse |
| 4 | +import code |
| 5 | +import sys |
| 6 | +import threading |
| 7 | +import websocket |
| 8 | +try: |
| 9 | + import readline |
| 10 | +except: |
| 11 | + pass |
| 12 | + |
| 13 | + |
| 14 | +OPCODE_DATA = (websocket.ABNF.OPCODE_TEXT, websocket.ABNF.OPCODE_BINARY) |
| 15 | +ENCODING = getattr(sys.stdin, "encoding", "").lower() |
| 16 | + |
| 17 | +class VAction(argparse.Action): |
| 18 | + def __call__(self, parser, args, values, option_string=None): |
| 19 | + if values==None: |
| 20 | + values = "1" |
| 21 | + try: |
| 22 | + values = int(values) |
| 23 | + except ValueError: |
| 24 | + values = values.count("v")+1 |
| 25 | + setattr(args, self.dest, values) |
| 26 | + |
| 27 | +def parse_args(): |
| 28 | + parser = argparse.ArgumentParser(description="WebSocket Simple Dump Tool") |
| 29 | + parser.add_argument("url", metavar="ws_url", |
| 30 | + help="websocket url. ex. ws://echo.websocket.org/") |
| 31 | + parser.add_argument("-v", "--verbose", default=0, nargs='?', action=VAction, |
| 32 | + dest="verbose", |
| 33 | + help="set verbose mode. If set to 1, show opcode. " |
| 34 | + "If set to 2, enable to trace websocket module") |
| 35 | + |
| 36 | + return parser.parse_args() |
| 37 | + |
| 38 | + |
| 39 | +class InteractiveConsole(code.InteractiveConsole): |
| 40 | + def write(self, data): |
| 41 | + sys.stdout.write("\033[2K\033[E") |
| 42 | + # sys.stdout.write("\n") |
| 43 | + sys.stdout.write("\033[34m" + data + "\033[39m") |
| 44 | + sys.stdout.write("\n> ") |
| 45 | + sys.stdout.flush() |
| 46 | + |
| 47 | + def raw_input(self, prompt): |
| 48 | + line = raw_input(prompt) |
| 49 | + if ENCODING and ENCODING != "utf-8" and not isinstance(line, unicode): |
| 50 | + line = line.decode(ENCODING).encode("utf-8") |
| 51 | + elif isinstance(line, unicode): |
| 52 | + line = encode("utf-8") |
| 53 | + |
| 54 | + return line |
| 55 | + |
| 56 | + |
| 57 | +def main(): |
| 58 | + args = parse_args() |
| 59 | + console = InteractiveConsole() |
| 60 | + if args.verbose > 1: |
| 61 | + websocket.enableTrace(True) |
| 62 | + ws = websocket.create_connection(args.url) |
| 63 | + print("Press Ctrl+C to quit") |
| 64 | + |
| 65 | + def recv(): |
| 66 | + frame = ws.recv_frame() |
| 67 | + if not frame: |
| 68 | + raise websocket.WebSocketException("Not a valid frame %s" % frame) |
| 69 | + elif frame.opcode in OPCODE_DATA: |
| 70 | + return (frame.opcode, frame.data) |
| 71 | + elif frame.opcode == websocket.ABNF.OPCODE_CLOSE: |
| 72 | + ws.send_close() |
| 73 | + return (frame.opcode, None) |
| 74 | + elif frame.opcode == websocket.ABNF.OPCODE_PING: |
| 75 | + ws.pong("Hi!") |
| 76 | + |
| 77 | + return None, None |
| 78 | + |
| 79 | + |
| 80 | + def recv_ws(): |
| 81 | + while True: |
| 82 | + opcode, data = recv() |
| 83 | + msg = None |
| 84 | + if not args.verbose and opcode in OPCODE_DATA: |
| 85 | + msg = "< %s" % data |
| 86 | + elif args.verbose: |
| 87 | + msg = "< %s: %s" % (websocket.ABNF.OPCODE_MAP.get(opcode), data) |
| 88 | + |
| 89 | + if msg: |
| 90 | + console.write(msg) |
| 91 | + |
| 92 | + thread = threading.Thread(target=recv_ws) |
| 93 | + thread.daemon = True |
| 94 | + thread.start() |
| 95 | + |
| 96 | + while True: |
| 97 | + try: |
| 98 | + message = console.raw_input("> ") |
| 99 | + ws.send(message) |
| 100 | + except KeyboardInterrupt: |
| 101 | + return |
| 102 | + except EOFError: |
| 103 | + return |
| 104 | + |
| 105 | + |
| 106 | +if __name__ == "__main__": |
| 107 | + try: |
| 108 | + main() |
| 109 | + except Exception as e: |
| 110 | + print(e) |
0 commit comments