From d342049ee42dd76a42dfc70be2efc8a71c940bb0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sim=C3=B3=20Albert=20i=20Beltran?= Date: Thu, 14 May 2015 17:16:15 +0200 Subject: [PATCH 1/3] wsdump.py writes empty string to console --- bin/wsdump.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/wsdump.py b/bin/wsdump.py index 6a9d1d8..993bad0 100755 --- a/bin/wsdump.py +++ b/bin/wsdump.py @@ -121,7 +121,7 @@ def main(): elif args.verbose: msg = "< %s: %s" % (websocket.ABNF.OPCODE_MAP.get(opcode), data) - if msg: + if msg is not None: console.write(msg) if opcode == websocket.ABNF.OPCODE_CLOSE: From d6168cfed15b3a34464ebe2248aa80075e64b184 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sim=C3=B3=20Albert=20i=20Beltran?= Date: Thu, 14 May 2015 17:18:43 +0200 Subject: [PATCH 2/3] console decoration of wsdump.py inside InteractiveConsole --- bin/wsdump.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/bin/wsdump.py b/bin/wsdump.py index 993bad0..875e10b 100755 --- a/bin/wsdump.py +++ b/bin/wsdump.py @@ -59,7 +59,7 @@ class InteractiveConsole(code.InteractiveConsole): def write(self, data): sys.stdout.write("\033[2K\033[E") # sys.stdout.write("\n") - sys.stdout.write("\033[34m" + data + "\033[39m") + sys.stdout.write("\033[34m< " + data + "\033[39m") sys.stdout.write("\n> ") sys.stdout.flush() @@ -117,9 +117,9 @@ def main(): opcode, data = recv() msg = None if not args.verbose and opcode in OPCODE_DATA: - msg = "< %s" % data + msg = data elif args.verbose: - msg = "< %s: %s" % (websocket.ABNF.OPCODE_MAP.get(opcode), data) + msg = "%s: %s" % (websocket.ABNF.OPCODE_MAP.get(opcode), data) if msg is not None: console.write(msg) From b7db6fd59c09036f78a87536f82a6bf97f7477fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sim=C3=B3=20Albert=20i=20Beltran?= Date: Thu, 14 May 2015 17:40:03 +0200 Subject: [PATCH 3/3] add raw mode to wsdump.py --- bin/wsdump.py | 40 ++++++++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/bin/wsdump.py b/bin/wsdump.py index 875e10b..d0678d4 100755 --- a/bin/wsdump.py +++ b/bin/wsdump.py @@ -45,6 +45,8 @@ def parse_args(): "If set to 2, enable to trace websocket module") parser.add_argument("-n", "--nocert", action='store_true', help="Ignore invalid SSL cert") + parser.add_argument("-r", "--raw", action="store_true", + help="raw output") parser.add_argument("-s", "--subprotocols", nargs='*', help="Set subprotocols") parser.add_argument("-o", "--origin", @@ -54,15 +56,7 @@ def parse_args(): return parser.parse_args() - -class InteractiveConsole(code.InteractiveConsole): - def write(self, data): - sys.stdout.write("\033[2K\033[E") - # sys.stdout.write("\n") - sys.stdout.write("\033[34m< " + data + "\033[39m") - sys.stdout.write("\n> ") - sys.stdout.flush() - +class RawInput(): def raw_input(self, prompt): if six.PY3: line = input(prompt) @@ -76,10 +70,28 @@ class InteractiveConsole(code.InteractiveConsole): return line +class InteractiveConsole(RawInput, code.InteractiveConsole): + def write(self, data): + sys.stdout.write("\033[2K\033[E") + # sys.stdout.write("\n") + sys.stdout.write("\033[34m< " + data + "\033[39m") + sys.stdout.write("\n> ") + sys.stdout.flush() + + def read(self): + return self.raw_input("> ") + +class NonInteractive(RawInput): + def write(self, data): + sys.stdout.write(data) + sys.stdout.write("\n") + sys.stdout.flush() + + def read(self): + return self.raw_input("") def main(): args = parse_args() - console = InteractiveConsole() if args.verbose > 1: websocket.enableTrace(True) options = {} @@ -91,7 +103,11 @@ def main(): if (args.nocert): opts = { "cert_reqs": websocket.ssl.CERT_NONE, "check_hostname": False } ws = websocket.create_connection(args.url, sslopt=opts, **options) - print("Press Ctrl+C to quit") + if args.raw: + console = NonInteractive() + else: + console = InteractiveConsole() + print("Press Ctrl+C to quit") def recv(): try: @@ -136,7 +152,7 @@ def main(): while True: try: - message = console.raw_input("> ") + message = console.read() ws.send(message) time.sleep(0.05) except KeyboardInterrupt: