コード例 #1
0
ファイル: tcpclient.py プロジェクト: aerpyljov/pyclientserver
 def run(self):
     '''
      Thread handler
     '''
     
     try:
         # Request creation
         data = Request('GET', 100)
         # Client socket binding
         sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
         sock.connect((self.__serverhost, self.__serverport))
         # Sending JSON data over the socket
         sock.send(data.to_json())
         print "[TCPClient][run] Request sent..."
         start = time.time()
         response = self.__receive_data(sock)
         end = time.time()
         if (self.__isdebug):
             print "Reception::Time Elapsed::", str(end - start)
             print "(Compressed) Dimension::", sys.getsizeof(response)
         # Treating compressed data
         result = self.__compression.decompress(response)
         data = Data(False, [], 0)
         data.from_json(result)
         if (int(data.nrbytes) == sys.getsizeof(data.vector)):
             print "[TCPClient][run] Integrity is OK"
         print "[TCPClient][run] Data\n", str(data.vector[:2])
     except Exception, e:
         print "Error::NET::sending exception [", str(e), "]"
コード例 #2
0
ファイル: tcpserver.py プロジェクト: zjjhebut/pyclientserver
    def handle(self):
        '''
         Service handler method
        '''

        try:
            print "[TCPRequestHandler][handle] Connection accepted... processing"
            # Reading request (assuming a small amount of bytes)
            data = self.request.recv(1024).strip()
            # Unmarshall the request
            request = Request('', 0)
            request.from_json(data)
            # Print data out, if debug
            if (self.__isdebug):
                print "Received data::", str(request)
            # Read lines from a text file
            resource = []
            resource.append(self.__resourcepath)
            resource.append(self.__filename)
            # Prepare the response data
            response = Data(True, [], 0)
            # Test if compressed content is needed
            list = []
            zippedfile = None
            testfile = None
            if (self.__compressedcontent):
                zippedfile = gzip.open(''.join(resource) + ".gz", "r+")
                list = zippedfile.readlines()
                print "(Compressed) Content size [", sys.getsizeof(list), "]"
            else:
                testfile = open(''.join(resource), 'r')
                list = testfile.readlines()
                print "(Uncompressed) Content size [", sys.getsizeof(list), "]"
            response.vector = list
            response.nrbytes = int(sys.getsizeof(list))
            # Marshall JSON representation
            json_str = response.to_json()
            if (self.__isdebug):
                print "(Original) JSON Dimension::", sys.getsizeof(json_str)
            c_response = self.__compression.compress(json_str)
            if (self.__isdebug):
                print "(Compressed) JSON Dimension::", sys.getsizeof(
                    c_response)
            start = time.time()
            self.request.sendall(c_response)
            print "[TCPRequestHandler][handle] Bunch of compressed data sent back!"
            end = time.time()
            if (self.__isdebug):
                print "Delivery::Time Elapsed::", str(end - start)
        except Exception, e:
            print "Exception upon message reception: ", e
コード例 #3
0
ファイル: tcpserver.py プロジェクト: aerpyljov/pyclientserver
 def handle(self):
     '''
      Service handler method
     '''
     
     try:
         print "[TCPRequestHandler][handle] Connection accepted... processing"
         # Reading request (assuming a small amount of bytes)
         data = self.request.recv(1024).strip()
         # Unmarshall the request
         request = Request('', 0)
         request.from_json(data)
         # Print data out, if debug
         if (self.__isdebug):
             print "Received data::", str(request)
         # Read lines from a text file
         resource = []
         resource.append(self.__resourcepath)
         resource.append(self.__filename)
         # Prepare the response data
         response = Data(True, [], 0)
         # Test if compressed content is needed
         list = []
         zippedfile = None
         testfile = None
         if (self.__compressedcontent):
             zippedfile = gzip.open(''.join(resource)+".gz", "r+")
             list = zippedfile.readlines()
             print "(Compressed) Content size [", sys.getsizeof(list), "]"
         else:
             testfile = open(''.join(resource), 'r')
             list = testfile.readlines()
             print "(Uncompressed) Content size [", sys.getsizeof(list), "]"
         response.vector = list
         response.nrbytes = int(sys.getsizeof(list))
         # Marshall JSON representation
         json_str = response.to_json()
         if (self.__isdebug):
             print "(Original) JSON Dimension::", sys.getsizeof(json_str)
         c_response = self.__compression.compress(json_str)
         if (self.__isdebug):
             print "(Compressed) JSON Dimension::", sys.getsizeof(c_response)
         start = time.time()
         self.request.sendall(c_response)
         print "[TCPRequestHandler][handle] Bunch of compressed data sent back!"
         end = time.time()
         if (self.__isdebug):
             print "Delivery::Time Elapsed::", str(end - start)
     except Exception, e:
         print "Exception upon message reception: ", e
コード例 #4
0
ファイル: tcpclient.py プロジェクト: zjjhebut/pyclientserver
    def run(self):
        '''
         Thread handler
        '''

        try:
            # Request creation
            data = Request('GET', 100)
            # Client socket binding
            sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            sock.connect((self.__serverhost, self.__serverport))
            # Sending JSON data over the socket
            sock.send(data.to_json())
            print "[TCPClient][run] Request sent..."
            start = time.time()
            response = self.__receive_data(sock)
            end = time.time()
            if (self.__isdebug):
                print "Reception::Time Elapsed::", str(end - start)
                print "(Compressed) Dimension::", sys.getsizeof(response)
            # Treating compressed data
            result = self.__compression.decompress(response)
            data = Data(False, [], 0)
            data.from_json(result)
            if (int(data.nrbytes) == sys.getsizeof(data.vector)):
                print "[TCPClient][run] Integrity is OK"
            print "[TCPClient][run] Data\n", str(data.vector[:2])
        except Exception, e:
            print "Error::NET::sending exception [", str(e), "]"