@@ -265,11 +265,10 @@ class Connection(object):
while True:
if not self.input:
error, data = self.stream.recv(4096)
- # Python 3 has separate types for strings and bytes. We
- # received bytes from a socket. We expect it to be string
- # data, so we convert it here as soon as possible.
- if (data and not error
- and not isinstance(data, six.string_types)):
+ # In Python 3, We received bytes from socket, and convert it
+ # to utf8 string by decode. In Python 2, We received ascii
+ # string from socket default, and convert it to utf8 by decode.
+ if data and not error:
try:
data = data.decode('utf-8')
except UnicodeError:
@@ -384,10 +384,10 @@ class Stream(object):
return self.__send_windows(buf)
try:
- # Python 3 has separate types for strings and bytes. We must have
- # bytes here.
- if six.PY3 and not isinstance(buf, six.binary_type):
- buf = six.binary_type(buf, 'utf-8')
+ # The buf type is usually string, we convert it to bytes by encode
+ # in python 3. and convert it to utf-8 string by encode in Python 2.
+ if isinstance(buf, six.text_type):
+ buf = buf.encode('utf-8')
return self.socket.send(buf)
except socket.error as e:
return -ovs.socket_util.get_exception_errno(e)
The OVSDB client may send or recv Chinese characters. So we should use UTF-8 encoding data for send or recv by default. Signed-off-by: Guoshuai Li <ligs@dtdream.com> --- python/ovs/jsonrpc.py | 9 ++++----- python/ovs/stream.py | 8 ++++---- 2 files changed, 8 insertions(+), 9 deletions(-)