summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDmitry Mottl <dmitry.mottl@gmail.com>2019-09-24 16:25:08 +0300
committerDmitry Mottl <dmitry.mottl@gmail.com>2019-09-24 16:25:08 +0300
commit9aa6a2698cf9bd5a5adfc45f99bb100bde60b057 (patch)
tree98e17d0ba94b9b166cbc208ce8cdcc92ffac9f73
parent3c25814664fef5b78716ed8841123ed1c0d17824 (diff)
downloadwebsocket-client-9aa6a2698cf9bd5a5adfc45f99bb100bde60b057.tar.gz
Show compressed text messages in wsdump.py
-rwxr-xr-xbin/wsdump.py26
1 files 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: