From 9aa6a2698cf9bd5a5adfc45f99bb100bde60b057 Mon Sep 17 00:00:00 2001 From: Dmitry Mottl Date: Tue, 24 Sep 2019 16:25:08 +0300 Subject: Show compressed text messages in wsdump.py --- bin/wsdump.py | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/bin/wsdump.py b/bin/wsdump.py index bc07246..026e125 100755 --- a/bin/wsdump.py +++ b/bin/wsdump.py @@ -6,6 +6,8 @@ import sys import threading import time import ssl +import gzip +import zlib import six from six.moves.urllib.parse import urlparse @@ -162,10 +164,28 @@ def main(): msg = None if six.PY3 and opcode == websocket.ABNF.OPCODE_TEXT and isinstance(data, bytes): data = str(data, "utf-8") - if not args.verbose and opcode in OPCODE_DATA: - msg = data - elif args.verbose: + if isinstance(data, bytes) and len(data)>2 and data[:2] == b'\037\213': # gzip magick + try: + data = "[gzip] " + str(gzip.decompress(data), "utf-8") + except: + pass + elif isinstance(data, bytes): + try: + decomp = zlib.decompressobj( + -zlib.MAX_WBITS + ) + data = decomp.decompress(data) + data = "[zlib] " + str(data + decomp.flush(), "utf-8") + except: + pass + + if isinstance(data, bytes): + data = repr(data) + + if args.verbose: msg = "%s: %s" % (websocket.ABNF.OPCODE_MAP.get(opcode), data) + else: + msg = data if msg is not None: if args.timings: -- cgit v1.2.1 From 79ae7ad3fd2f45724abca76fba3668c9357095b4 Mon Sep 17 00:00:00 2001 From: Dmitry Mottl Date: Wed, 25 Sep 2019 14:48:36 +0300 Subject: Simplify zlib.decompress call --- bin/wsdump.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/bin/wsdump.py b/bin/wsdump.py index 026e125..5cdb8d8 100755 --- a/bin/wsdump.py +++ b/bin/wsdump.py @@ -171,11 +171,7 @@ def main(): pass elif isinstance(data, bytes): try: - decomp = zlib.decompressobj( - -zlib.MAX_WBITS - ) - data = decomp.decompress(data) - data = "[zlib] " + str(data + decomp.flush(), "utf-8") + data = "[zlib] " + str(zlib.decompress(data, -zlib.MAX_WBITS), "utf-8") except: pass -- cgit v1.2.1