def action(self, action, args, kwargs): # It's really pathetic, but it's still debugging output. self.log.debug('Action %r called with %d args', action, len(args) + len(kwargs)) # This method is responsible for the JSON encoding/decoding, not send(). # This was deliberate because it keeps most of the protocol details # separate from the lower-level socket code. received_data = self.send(json.dumps([action, args, kwargs]) + '\r\n') return self.handle_response(received_data)
def close(self): # The following closes the connection by first sending the 'quit' # message and then closing the socket via the forced _close() method. self.lock.acquire() try: self.writer.write(json.dumps(['quit']) + '\r\n') self._close() except Exception, exc: self.log.error('Error %r occurred while closing connection', exc) raise
def action(self, action, args, kwargs): # It's really pathetic, but it's still debugging output. self.log.debug('Action %r called with %d args', action, len(args) + len(kwargs)) path = '/' + urllib.quote(action) + '/' url = URLObject(host=self.host).with_port(self.port).with_path(path) received_data = self.send(url, data=json.dumps([args, kwargs])) return self.handle_response(received_data)
def write_json(writer, object): # A simple utility method. writer.write(json.dumps(object) + '\r\n')
def __new__(cls, obj, status=200): return Response(response=json.dumps(obj), mimetype='application/json', status=status)