def handle(self, client):
        reader, writer = client.makefile('r'), client.makefile('w')
        
        try:
            while True:
                try:
                    # If the client sends an empty line, ignore it.
                    line = reader.readline()
                    stripped_line = line.rstrip('\r\n')
                    if not line:
                        break
                    elif not stripped_line:
                        api.sleep(0)
                        continue
                
                    # Try to parse the request, failing if it is invalid.
                    try:
                        action, args, kwargs = self.parse_command(stripped_line)
                    except ValueError:
                        # Request was malformed. ValueError is raised by
                        # simplejson when the passed string is not valid JSON.
                        self.log.error('Received malformed request from client %x',
                            id(client))
                        write_json(writer, ['error:request', 'malformed request'])
                        continue
                
                    # Find the method corresponding to the requested action.
                    try:
                        method = getattr(self, 'do_' + action)
                    except __HOLE__:
                        self.log.error('Missing action requested by client %x',
                            id(client))
                        write_json(writer, ['error:request', 'action not found'])
                        continue
                
                    # Run the method, dealing with exceptions or success.
                    try:
                        self.log.debug('Action %r requested by client %x',
                            action, id(client))
                        # All actions get the client socket as an additional
                        # argument. This means they can do cool things with the
                        # client object that might not be possible otherwise.
                        output = method(client, *args, **kwargs)
                    except Break:
                        # The Break error propagates up the call chain and
                        # causes the server to disconnect the client.
                        break
                    except self.queue.Timeout:
                        # The client will pick this up. It's not so much a
                        # serious error, which is why we don't log it: timeouts
                        # are more often than not specified for very useful
                        # reasons.
                        write_json(writer, ['error:timeout', None])
                    except Exception, exc:
                        self.log.error(
                            'Action %r raised error %r for client %x',
                            action, exc, id(client))
                        write_json(writer, ['error:action', repr(exc)])
                        # Chances are that if an error occurred, we'll need to
                        # raise it properly. This will trigger the closing of
                        # the client socket via the finally clause below.
                        raise ActionError(exc)
                    else:
                        # I guess debug is overkill.
                        self.log.debug('Action %r successful for client %x',
                            action, id(client))
                        write_json(writer, ['success', output])
                except ActionError, exc:
                    # Raise the inner action error. This will prevent the
                    # catch-all except statement below from logging action
                    # errors as 'unknown' errors. The exception has already been
                    # written to the client.
                    raise ActionError.args[0]
                except Exception, exc:
                    self.log.error('Unknown error occurred for client %x: %r',
                        id(client), exc)
                    # If we really don't know what happened, then
                    write_json(writer, ['error:unknown', repr(exc)])
                    raise # Raises the last exception, in this case exc.
        except:
            # If any exception has been raised at this point, it will show up as
            # an error in the logging output.
            self.log.error('Forcing disconnection of client %x', id(client))
        finally:
            # If code reaches this point simply by non-error means (i.e. an
            # actual call to the quit, exit or shutdown actions), then it will
            # not include an error-level logging event.
            self.log.info('Client %x disconnected', id(client))
            client.close()
    
    # Most of these methods are pure wrappers around the underlying queue
    # object.
dataset/ETHPy150Open zacharyvoase/zenqueue/zenqueue/server/native.py/NativeQueueServer.handle