From 187e8bdd4d1ff488b82c4134407730adece35835 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Louis=20V=C3=A9zina?= <5130500+morpheus65535@users.noreply.github.com> Date: Fri, 7 Feb 2020 15:41:23 -0500 Subject: [PATCH] Fix for #739 and #798 --- libs/deathbycaptcha.py | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/libs/deathbycaptcha.py b/libs/deathbycaptcha.py index 05aa8f68d..0a6c43b06 100644 --- a/libs/deathbycaptcha.py +++ b/libs/deathbycaptcha.py @@ -80,6 +80,7 @@ import sys import threading import time import urllib +from six import PY2 try: from json import read as json_decode, write as json_encode except ImportError: @@ -265,7 +266,10 @@ class HttpClient(Client): else: self._log('RECV', '%d %s' % (len(response), response)) try: - return json_decode(response) + if PY2: + return json_decode(response.decode('utf-8')) + else: + return json_decode(response) except Exception: raise RuntimeError('Invalid API response') return {} @@ -369,8 +373,8 @@ class SocketClient(Client): def _sendrecv(self, sock, buf): self._log('SEND', buf) fds = [sock] - buf += self.TERMINATOR - response = '' + buf.extend(bytearray(self.TERMINATOR, encoding='utf-8')) + response = bytearray() intvl_idx = 0 while True: intvl, intvl_idx = self._get_poll_interval(intvl_idx) @@ -390,14 +394,14 @@ class SocketClient(Client): if not s: raise IOError('recv(): connection lost') else: - response += s + response.extend(s) except socket.error as err: if (err.args[0] not in (errno.EAGAIN, errno.EWOULDBLOCK, errno.EINPROGRESS)): raise err - if response.endswith(self.TERMINATOR): + if response.endswith(self.TERMINATOR.encode('utf-8')): self._log('RECV', response) - return response.rstrip(self.TERMINATOR) + return response.rstrip(self.TERMINATOR.encode('utf-8')) raise IOError('send/recv timed out') def _call(self, cmd, data=None): @@ -405,7 +409,7 @@ class SocketClient(Client): data = {} data['cmd'] = cmd data['version'] = API_VERSION - request = json_encode(data) + request = bytearray(json_encode(data), encoding='utf-8') response = None for _ in range(2): @@ -431,7 +435,10 @@ class SocketClient(Client): raise IOError('Connection lost or timed out during API request') try: - response = json_decode(response) + if PY2: + return json_decode(response.decode('utf-8')) + else: + return json_decode(response) except Exception: raise RuntimeError('Invalid API response')