Esempio n. 1
0
def _socket_parse(**kwargs):
    """Send a remote parsing request and get a response."""
    # Get the socket out of kwargs and send over the rest
    sock = kwargs.pop('asocket')
    msg = json.dumps(kwargs) + MSG_SEP
    print "Sending:", repr(msg)
    sock.sendall(msg)
    print "Waiting for response..."
    buff = sock.recv(4096)
    msg, buff = _parse_msg(buff, MSG_SEP)
    return msg
Esempio n. 2
0
def _socket_parse(**kwargs):
    """Send a remote parsing request and get a response."""
    # Get the socket and verbose out of kwargs and send over the rest
    sock = kwargs.pop('asocket')
    verbose = kwargs.pop('verbose')
    msg = json.dumps(kwargs) + MSG_SEP
    if verbose:
        print "Sending:", repr(msg)
    sock.sendall(msg)
    if verbose:
        print "Waiting for response..."
    buff = sock.recv(4096)
    msg, buff = _parse_msg(buff, MSG_SEP)
    return msg
Esempio n. 3
0
    def _handle_client(self, conn, name):
        """Process requests from a client."""
        send_error = False
        while True:
            try:
                print "Waiting for data..."
                buff = conn.recv(4096)
            except timeout:
                continue
            # pylint: disable=W0702
            except:
                # Break the connection for all more serious errors
                break
            if not buff:
                break
            while buff:
                if self.msg_sep:
                    msg, buff = _parse_msg(buff, self.msg_sep)
                else:
                    msg, buff = buff, None
                if msg:
                    response = self._process_text(msg)
                    if response:
                        print "Sending:", response
                        try:
                            conn.sendall(json.dumps(response))
                        except:
                            # Give up on the client entirely
                            print "%s: Error sending message." % self.name
                            send_error = True
                            break
                else:
                    print "%s: Received an incomplete message: %s" % (self.name, repr(buff))
                    break

            # Break out if there was a sending error
            if send_error:
                break

        print "%s: %s disconnected." % (self.name, name)