OLD | NEW |
| (Empty) |
1 #!/usr/bin/env 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 | |
48 def raw_input(self, prompt): | |
49 line = raw_input(prompt) | |
50 if ENCODING and ENCODING != "utf-8" and not isinstance(line, unicode): | |
51 line = line.decode(ENCODING).encode("utf-8") | |
52 elif isinstance(line, unicode): | |
53 line = encode("utf-8") | |
54 | |
55 return line | |
56 | |
57 | |
58 def main(): | |
59 args = parse_args() | |
60 console = InteractiveConsole() | |
61 ws = websocket.create_connection(args.url) | |
62 if args.verbose > 1: | |
63 websocket.enableTrace(True) | |
64 print "Press Ctrl+C to quit" | |
65 | |
66 def recv(): | |
67 frame = ws.recv_frame() | |
68 if not frame: | |
69 raise websocket.WebSocketException("Not a valid frame %s" % frame) | |
70 elif frame.opcode in OPCODE_DATA: | |
71 return (frame.opcode, frame.data) | |
72 elif frame.opcode == websocket.ABNF.OPCODE_CLOSE: | |
73 ws.send_close() | |
74 return (frame.opcode, None) | |
75 elif frame.opcode == websocket.ABNF.OPCODE_PING: | |
76 ws.pong("Hi!") | |
77 | |
78 return None, None | |
79 | |
80 | |
81 def recv_ws(): | |
82 while True: | |
83 opcode, data = recv() | |
84 msg = None | |
85 if not args.verbose and opcode in OPCODE_DATA: | |
86 msg = "< %s" % data | |
87 elif args.verbose: | |
88 msg = "< %s: %s" % (websocket.ABNF.OPCODE_MAP.get(opcode), data) | |
89 | |
90 if msg: | |
91 console.write(msg) | |
92 | |
93 thread = threading.Thread(target=recv_ws) | |
94 thread.daemon = True | |
95 thread.start() | |
96 | |
97 while True: | |
98 try: | |
99 message = console.raw_input("> ") | |
100 ws.send(message) | |
101 except KeyboardInterrupt: | |
102 return | |
103 except EOFError: | |
104 return | |
105 | |
106 | |
107 if __name__ == "__main__": | |
108 try: | |
109 main() | |
110 except Exception, e: | |
111 print e | |
OLD | NEW |