From db384547874f9ea420fe345672d17e41252d27a5 Mon Sep 17 00:00:00 2001 From: vapao Date: Thu, 14 Jul 2022 19:46:47 +0800 Subject: [PATCH] =?UTF-8?q?F=20=E4=BF=AE=E5=A4=8Dweb=E7=BB=88=E7=AB=AF?= =?UTF-8?q?=E5=81=B6=E7=8E=B0=E4=B8=AD=E6=96=87=E4=B9=B1=E7=A0=81=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- spug_api/consumer/consumers.py | 20 ++++++++++++++++---- spug_api/libs/utils.py | 5 ++++- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/spug_api/consumer/consumers.py b/spug_api/consumer/consumers.py index 91b5554..f150bb9 100644 --- a/spug_api/consumer/consumers.py +++ b/spug_api/consumer/consumers.py @@ -59,17 +59,29 @@ class SSHConsumer(BaseConsumer): self.ssh = None def loop_read(self): - is_ready = False + is_ready, data = False, b'' while True: - data = self.chan.recv(32 * 1024) - if not data: + out = self.chan.recv(32 * 1024) + if not out: self.close(3333) break - text = str_decode(data) + data += out + try: + text = data.decode() + except UnicodeDecodeError: + try: + text = data.decode(encoding='GBK') + except UnicodeDecodeError: + time.sleep(0.01) + if self.chan.recv_ready(): + continue + text = data.decode(errors='ignore') + if not is_ready: self.send(text_data='\033[2J\033[3J\033[1;1H') is_ready = True self.send(text_data=text) + data = b'' def receive(self, text_data=None, bytes_data=None): data = text_data or bytes_data diff --git a/spug_api/libs/utils.py b/spug_api/libs/utils.py index 050d944..aba6f99 100644 --- a/spug_api/libs/utils.py +++ b/spug_api/libs/utils.py @@ -41,7 +41,10 @@ def str_decode(data): try: data = data.decode() except UnicodeDecodeError: - data = data.decode(encoding='GBK', errors='ignore') + try: + data = data.decode(encoding='GBK') + except UnicodeDecodeError: + data = data.decode(errors='ignore') return data