def _dispatch(self, method, params): func = None try: func = self.funcs[method] except KeyError: if self.instance is not None: if hasattr(self.instance, '_dispatch'): return self.instance._dispatch(method, params) else: try: func = SimpleXMLRPCServer.resolve_dotted_attribute( self.instance, method, True) except AttributeError: pass if func is not None: try: if type(params) is types.ListType: response = func(*params) else: response = func(**params) return response except TypeError: return Fault(-32602, 'Invalid parameters.') except: err_lines = traceback.format_exc().splitlines() trace_string = '%s | %s' % (err_lines[-3], err_lines[-1]) fault = jsonrpclib.Fault(-32603, 'Server error: %s' % trace_string) return fault else: return Fault(-32601, 'Method %s not supported.' % method)
def _marshaled_single_dispatch(self, request): # TODO - Use the multiprocessing and skip the response if # it is a notification # Put in support for custom dispatcher here # (See SimpleXMLRPCServer._marshaled_dispatch) method = request.get('method') params = request.get('params') try: response = self._dispatch(method, params) except: exc_type, exc_value, exc_tb = sys.exc_info() fault = Fault(-32603, '%s:%s' % (exc_type, exc_value)) return fault.response() if 'id' not in request.keys() or request['id'] == None: # It's a notification return None try: response = jsonrpclib.dumps(response, methodresponse=True, rpcid=request['id'] ) return response except: exc_type, exc_value, exc_tb = sys.exc_info() fault = Fault(-32603, '%s:%s' % (exc_type, exc_value)) return fault.response()
def _marshaled_dispatch(self, data, dispatch_method = None): response = None try: request = jsonrpclib.loads(data) except Exception, e: fault = Fault(-32700, 'Request %s invalid. (%s)' % (data, e)) response = fault.response() return response
def _marshaled_dispatch(self, data, dispatch_method=None): response = None try: request = jsonrpclib.loads(data) except Exception, e: fault = Fault(-32700, 'Request %s invalid. (%s)' % (data, e)) response = fault.response() return response
def validate_request(request): if type(request) is not types.DictType: fault = Fault(-32600, 'Request must be {}, not %s.' % type(request)) return fault rpcid = request.get('id', None) version = get_version(request) if not version: fault = Fault(-32600, 'Request %s invalid.' % request, rpcid=rpcid) return fault request.setdefault('params', []) method = request.get('method', None) params = request.get('params') param_types = (types.ListType, types.DictType, types.TupleType) if not method or type(method) not in types.StringTypes or \ type(params) not in param_types: fault = Fault(-32600, 'Invalid request parameters or method.', rpcid=rpcid) return fault return True
def _marshaled_single_dispatch(self, request): # TODO - Use the multiprocessing and skip the response if # it is a notification # Put in support for custom dispatcher here # (See SimpleXMLRPCServer._marshaled_dispatch) method = request.get('method') params = request.get('params') try: response = self._dispatch(method, params) except: exc_type, exc_value, exc_tb = sys.exc_info() fault = Fault(-32603, '%s:%s' % (exc_type, exc_value)) return fault.response() if 'id' not in request.keys() or request['id'] == None: # It's a notification return None try: response = jsonrpclib.dumps(response, methodresponse=True, rpcid=request['id']) return response except: exc_type, exc_value, exc_tb = sys.exc_info() fault = Fault(-32603, '%s:%s' % (exc_type, exc_value)) return fault.response()
class SimpleJSONRPCDispatcher(SimpleXMLRPCServer.SimpleXMLRPCDispatcher): def __init__(self, encoding=None): SimpleXMLRPCServer.SimpleXMLRPCDispatcher.__init__(self, allow_none=True, encoding=encoding) def _marshaled_dispatch(self, data, dispatch_method = None): response = None try: request = jsonrpclib.loads(data) except Exception, e: fault = Fault(-32700, 'Request %s invalid. (%s)' % (data, e)) response = fault.response() return response if not request: fault = Fault(-32600, 'Request invalid -- no request data.') return fault.response() if type(request) is types.ListType: # This SHOULD be a batch, by spec responses = [] for req_entry in request: result = validate_request(req_entry) if type(result) is Fault: responses.append(result.response()) continue resp_entry = self._marshaled_single_dispatch(req_entry) if resp_entry is not None: responses.append(resp_entry) if len(responses) > 0: response = '[%s]' % ','.join(responses) else: response = '' else: result = validate_request(request) if type(result) is Fault: return result.response() response = self._marshaled_single_dispatch(request) return response