diff --git a/server/gabriel/common/network/TCP.py b/server/gabriel/common/network/TCP.py index a1d79b4c..4d83de3a 100644 --- a/server/gabriel/common/network/TCP.py +++ b/server/gabriel/common/network/TCP.py @@ -189,7 +189,7 @@ def _recv_all(self, recv_size): ''' Received data till a specified size. ''' - data = '' + data = emptydata() while len(data) < recv_size: tmp_data = self.sock.recv(recv_size - len(data)) if tmp_data is None: diff --git a/server/gabriel/proxy/common.py b/server/gabriel/proxy/common.py index 96c1662b..bd181965 100644 --- a/server/gabriel/proxy/common.py +++ b/server/gabriel/proxy/common.py @@ -22,7 +22,10 @@ import json import multiprocessing -import Queue +try: + import Queue +except ImportError: + import queue as Queue import select import socket import struct @@ -34,6 +37,17 @@ import gabriel LOG = gabriel.logging.getLogger(__name__) +try: + str(b'0x1','ascii') + def mystr(b): + return str(b, 'ascii') + def bts(s): + return bytes(s, 'ascii') +except: + def mystr(b): + return str(b) + def bts(s): + return bytes(s) class ProxyError(Exception): pass @@ -57,7 +71,7 @@ def _handle_input_data(self): data_size = struct.unpack("!I", self._recv_all(4))[0] header_str = self._recv_all(header_size) data = self._recv_all(data_size) - header_json = json.loads(header_str) + header_json = json.loads(mystr(header_str)) # add header data for measurement if gabriel.Debug.TIME_MEASUREMENT: @@ -179,7 +193,7 @@ def _handle_queue_data(self): rtn_header, rtn_data = self.data_queue.get(timeout = 0.0001) total_size = len(rtn_header) + len(rtn_data) # packet format: total size, header size, header, data - packet = struct.pack("!II{}s{}s".format(len(rtn_header), len(rtn_data)), total_size, len(rtn_header), rtn_header, rtn_data) + packet = struct.pack("!II{}s{}s".format(len(rtn_header), len(rtn_data)), total_size, len(rtn_header), bts(rtn_header), rtn_data) self.sock.sendall(packet) LOG.info("sending result to ucomm: %s" % gabriel.util.print_rtn(json.loads(rtn_header))) except Queue.Empty as e: @@ -213,14 +227,14 @@ def _handle_queue_data(self): header_str = json.dumps(header) # send data - packet = struct.pack("!II%ds%ds" % (len(header_str), len(data)), len(header_str), len(data), header_str, data) + packet = struct.pack("!II%ds%ds" % (len(header_str), len(data)), len(header_str), len(data), bts(header_str), data) self.request.send(packet) self.wfile.flush() # receive result header_size = struct.unpack("!I", self._recv_all(4))[0] header_str = self._recv_all(header_size) - header = json.loads(header_str) + header = json.loads(mystr(header_str)) state_size = struct.unpack("!I", self._recv_all(4))[0] state = self._recv_all(state_size)