mirror of
https://github.com/morpheus65535/bazarr.git
synced 2025-01-06 06:52:07 +08:00
parent
d0d8388d9f
commit
187e8bdd4d
1 changed files with 15 additions and 8 deletions
|
@ -80,6 +80,7 @@ import sys
|
||||||
import threading
|
import threading
|
||||||
import time
|
import time
|
||||||
import urllib
|
import urllib
|
||||||
|
from six import PY2
|
||||||
try:
|
try:
|
||||||
from json import read as json_decode, write as json_encode
|
from json import read as json_decode, write as json_encode
|
||||||
except ImportError:
|
except ImportError:
|
||||||
|
@ -265,7 +266,10 @@ class HttpClient(Client):
|
||||||
else:
|
else:
|
||||||
self._log('RECV', '%d %s' % (len(response), response))
|
self._log('RECV', '%d %s' % (len(response), response))
|
||||||
try:
|
try:
|
||||||
return json_decode(response)
|
if PY2:
|
||||||
|
return json_decode(response.decode('utf-8'))
|
||||||
|
else:
|
||||||
|
return json_decode(response)
|
||||||
except Exception:
|
except Exception:
|
||||||
raise RuntimeError('Invalid API response')
|
raise RuntimeError('Invalid API response')
|
||||||
return {}
|
return {}
|
||||||
|
@ -369,8 +373,8 @@ class SocketClient(Client):
|
||||||
def _sendrecv(self, sock, buf):
|
def _sendrecv(self, sock, buf):
|
||||||
self._log('SEND', buf)
|
self._log('SEND', buf)
|
||||||
fds = [sock]
|
fds = [sock]
|
||||||
buf += self.TERMINATOR
|
buf.extend(bytearray(self.TERMINATOR, encoding='utf-8'))
|
||||||
response = ''
|
response = bytearray()
|
||||||
intvl_idx = 0
|
intvl_idx = 0
|
||||||
while True:
|
while True:
|
||||||
intvl, intvl_idx = self._get_poll_interval(intvl_idx)
|
intvl, intvl_idx = self._get_poll_interval(intvl_idx)
|
||||||
|
@ -390,14 +394,14 @@ class SocketClient(Client):
|
||||||
if not s:
|
if not s:
|
||||||
raise IOError('recv(): connection lost')
|
raise IOError('recv(): connection lost')
|
||||||
else:
|
else:
|
||||||
response += s
|
response.extend(s)
|
||||||
except socket.error as err:
|
except socket.error as err:
|
||||||
if (err.args[0] not in
|
if (err.args[0] not in
|
||||||
(errno.EAGAIN, errno.EWOULDBLOCK, errno.EINPROGRESS)):
|
(errno.EAGAIN, errno.EWOULDBLOCK, errno.EINPROGRESS)):
|
||||||
raise err
|
raise err
|
||||||
if response.endswith(self.TERMINATOR):
|
if response.endswith(self.TERMINATOR.encode('utf-8')):
|
||||||
self._log('RECV', response)
|
self._log('RECV', response)
|
||||||
return response.rstrip(self.TERMINATOR)
|
return response.rstrip(self.TERMINATOR.encode('utf-8'))
|
||||||
raise IOError('send/recv timed out')
|
raise IOError('send/recv timed out')
|
||||||
|
|
||||||
def _call(self, cmd, data=None):
|
def _call(self, cmd, data=None):
|
||||||
|
@ -405,7 +409,7 @@ class SocketClient(Client):
|
||||||
data = {}
|
data = {}
|
||||||
data['cmd'] = cmd
|
data['cmd'] = cmd
|
||||||
data['version'] = API_VERSION
|
data['version'] = API_VERSION
|
||||||
request = json_encode(data)
|
request = bytearray(json_encode(data), encoding='utf-8')
|
||||||
|
|
||||||
response = None
|
response = None
|
||||||
for _ in range(2):
|
for _ in range(2):
|
||||||
|
@ -431,7 +435,10 @@ class SocketClient(Client):
|
||||||
raise IOError('Connection lost or timed out during API request')
|
raise IOError('Connection lost or timed out during API request')
|
||||||
|
|
||||||
try:
|
try:
|
||||||
response = json_decode(response)
|
if PY2:
|
||||||
|
return json_decode(response.decode('utf-8'))
|
||||||
|
else:
|
||||||
|
return json_decode(response)
|
||||||
except Exception:
|
except Exception:
|
||||||
raise RuntimeError('Invalid API response')
|
raise RuntimeError('Invalid API response')
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue