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