Beispiel #1
0
    def _send_and_receive(self, message, batch_count=None):
        """
        Handles the socket connection, sends the JSON request, and
        (if not a notification) retrieves the response and decodes the
        JSON text.
        """
        # Starting with a clean history
        history.request = message
        logger.debug('CLIENT | REQUEST: %s' % message)

        if self._sock is not None:
            err = self.__try_send(message)
            if err is not None:
                self._close()

        if self._sock is None:
            self._sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            self._sock.settimeout(config.timeout)
            self._sock.connect(self._addr)

            err = self.__try_send(message)
            if err is not None:
                self._close()
                raise err

        responses = self._recv(1 if batch_count is None else batch_count)

        if batch_count is None:
            responses = responses[0]

        logger.debug('CLIENT | RESPONSE: %s' % responses)
        history.response = responses
        return responses
Beispiel #2
0
    def _send_and_receive(self, message, batch=False, notify=False):
        """
        Handles the socket connection, sends the JSON request, and
        (if not a notification) retrieves the response and decodes the
        JSON text.
        """
        # Starting with a clean history
        history.request = message
        logger.debug('CLIENT | REQUEST: %s' % message)

        if self._key:
            crypt = config.crypt.new(self._key)
            length = config.crypt_chunk_size
            pad_length = length - (len(message) % length)
            message = crypt.encrypt('%s%s' % (message, ' ' * pad_length))
        print(message)
        if type(message) == type("message"):  # encode
            message = message.encode("utf-8")

        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock.settimeout(config.timeout)
        sock.connect(self._addr)
        sock.send(message)

        responselist = []
        if notify:
            # single notification, we don't need a response.
            sock.close()
        else:
            while True:
                try:
                    data = sock.recv(config.buffer)
                except socket.timeout:
                    break
                if not data:
                    break
                if type(data) != type("data"):
                    data = data.decode("utf-8")
                responselist.append(data)
                if len(data) < config.buffer:
                    break
            sock.close()
        response = ''.join(responselist)
        if self._key:
            try:
                response = crypt.decrypt(response)
            except ValueError:
                # What exactly is an intuitive response to a poorly- or
                # not-encrypted response to an encrypted request?
                raise ProtocolError(-32700, 'Response not encrypted properly.')
            # Should we do a preliminary json.loads here to verify that the
            # decryption succeeded?
        logger.debug('CLIENT | RESPONSE: %s' % response)
        history.response = response
        return response
Beispiel #3
0
 def _send_and_receive(self, message, batch=False, notify=False):
     """
     Handles the socket connection, sends the JSON request, and
     (if not a notification) retrieves the response and decodes the
     JSON text.
     """
     # Starting with a clean history
     history.request = message
     logger.debug('CLIENT | REQUEST: %s' % message)
     if self._key:
         crypt = config.crypt.new(self._key)
         length = config.crypt_chunk_size
         pad_length = length - (len(message) % length)
         message = crypt.encrypt('%s%s' % (message, ' '*pad_length))
     if config.append_string:
         message += config.append_string
     sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
     sock.settimeout(config.timeout)
     sock.connect(self._addr)
     sock.send(message)
     
     responselist = []
     if notify:
         # single notification, we don't need a response.
         sock.close()
     else:
         while True:
             try:
                 data = sock.recv(config.buffer)
             except socket.timeout:
                 break
             if not data: 
                 break
             responselist.append(data)
             if len(data) < config.buffer:
                 break
         sock.close()
     response = ''.join(responselist)
     if self._key:
         try:
             response = crypt.decrypt(response)
         except ValueError:
             # What exactly is an intuitive response to a poorly- or
             # not-encrypted response to an encrypted request?
             raise ProtocolError(-32700, 'Response not encrypted properly.')
         # Should we do a preliminary json.loads here to verify that the
         # decryption succeeded?
     logger.debug('CLIENT | RESPONSE: %s' % response)
     history.response = response
     return response
Beispiel #4
0
    def process(self, sock, addr):
        """
        Retrieves the data stream from the socket and validates it.
        """
        self.socket = sock
        self.socket.settimeout(config.timeout)
        self.client_address = addr
        requestlines = []
        while True:
            data = self.get_data()
            if not data:
                break
            if type(data) != type('data'):
                data = data.decode('utf-8')
            requestlines.append(data)
            if len(data) < config.buffer:
                break
        request = ''.join(requestlines)
        response = ''
        crypt_error = False
        if config.secret:
            crypt = config.crypt.new(config.secret)
            try:
                request = crypt.decrypt(request)
            except ValueError:
                crypt_error = True
                error = ProtocolError(-32700, 'Could not decrypt request.')
                response = json.dumps(error.generate_error())
        history.request = request
        logger.debug('SERVER | REQUEST: %s' % request)
        if self.socket_error:
            self.socket.close()
        else:
            if not crypt_error:
                response = self.parse_request(request)
            history.response = response
            logger.debug('SERVER | RESPONSE: %s' % response)
            if config.secret:
                length = config.crypt_chunk_size
                pad_length = length - (len(response) % length)
                response = crypt.encrypt('%s%s' % (response, ' ' * pad_length))

            if type(response) == type('response'):
                response = response.encode('utf-8')
            self.socket.send(response)
        self.socket.close()
Beispiel #5
0
 def process(self, sock, addr):
     """
     Retrieves the data stream from the socket and validates it.
     """
     self.socket = sock
     self.socket.settimeout(config.timeout)
     self.client_address = addr
     requestlines = []
     while True:
         data = self.get_data()
         if not data: 
             break
         requestlines.append(data)
         if len(data) < config.buffer: 
             break
     request = ''.join(requestlines)
     response = ''
     crypt_error = False
     if config.secret:
         crypt = config.crypt.new(config.secret)
         try:
             request = crypt.decrypt(request)
         except ValueError:
             crypt_error = True
             error = ProtocolError(-32700, 'Could not decrypt request.')
             response = json.dumps(error.generate_error())
     history.request = request
     logger.debug('SERVER | REQUEST: %s' % request)
     if self.socket_error:
         self.socket.close()
     else:
         if not crypt_error:
             response = self.parse_request(request)
         history.response = response
         logger.debug('SERVER | RESPONSE: %s' % response)
         if config.secret:
             length = config.crypt_chunk_size
             pad_length = length - (len(response) % length)
             response = crypt.encrypt('%s%s' % (response, ' '*pad_length))
         self.socket.send(response)
     self.socket.close()