From 015fdfc41398460d0a1ba356707aece681456ca8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E7=A6=BB?= Date: Sat, 20 Apr 2019 09:53:45 +0800 Subject: [PATCH] Fix python3 UnicodeDecodeError when use websocket --- stream/ws_client.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/stream/ws_client.py b/stream/ws_client.py index cf8a3fe9..7705627a 100644 --- a/stream/ws_client.py +++ b/stream/ws_client.py @@ -24,6 +24,7 @@ import six import ssl from six.moves.urllib.parse import urlencode, quote_plus, urlparse, urlunparse +import chardet STDIN_CHANNEL = 0 STDOUT_CHANNEL = 1 @@ -181,7 +182,8 @@ def update(self, timeout=0): elif op_code == ABNF.OPCODE_BINARY or op_code == ABNF.OPCODE_TEXT: data = frame.data if six.PY3: - data = data.decode("utf-8") + encode = chardet.detect(data).get('encoding') + data = data.decode(encode or "utf-8") if len(data) > 1: channel = ord(data[0]) data = data[1:]