def respond(result): if isinstance(result, Exception): # Package exception into XML-RPC Fault instance. message = getattr(result, "message", "") if not message: message = str(result) typename = type(result).__name__ if typename == "instance": typename = result.__class__.__name__ faultstring = "%s: %s" % (typename, message) if isinstance(faultstring, unicode): try: faultstring = str(faultstring) except UnicodeEncodeError: faultstring = faultstring.encode('utf') result = xmlrpclib.Fault(1, faultstring) else: # Dumps requires tuple or Fault type. result = (result, ) xmlresult = xmlrpclib.dumps(result, methodresponse=True) response = Response(request) response.set_header('Content-Length', len(xmlresult)) response.set_header('Content-Type', 'text/xml') response.send(xmlresult) self.debugout("Response sent to client.")
def respond(result): if isinstance(result, Exception): # Package exception into XML-RPC Fault instance. message = getattr(result, "message", "") if not message: message = str(result) typename = type(result).__name__ if typename == "instance": typename = result.__class__.__name__ faultstring = "%s: %s" % (typename, message) if isinstance(faultstring, unicode): try: faultstring = str(faultstring) except UnicodeEncodeError: faultstring = faultstring.encode('utf') result = xmlrpclib.Fault(1, faultstring) else: # Dumps requires tuple or Fault type. result = (result,) xmlresult = xmlrpclib.dumps(result, methodresponse=True) response = Response(request) response.set_header('Content-Length', len(xmlresult)) response.set_header('Content-Type', 'text/xml') response.send(xmlresult) self.debugout("Response sent to client.")
def handle_request(self, request): path = request.get_path().split('/') # if there is a child to call, should be the # 3rd element path if len(path) < 3: fault_xml = xmlrpclib.dumps(xmlrpclib.Fault(1, "Invalid URL Path")) response = Response(request) response.send(fault_xml) else: c = path[2] handler = self.get_child(c) handler.handle_request(request)
def handle_request(self, request): path = request.get_path().split('/') # if there is a child to call, should be the # 3rd element path if len(path) < 3: fault_xml = xmlrpclib.dumps( xmlrpclib.Fault(1, "Invalid URL Path") ) response = Response(request) response.send(fault_xml) else: c = path[2] handler = self.get_child(c) handler.handle_request(request)
def write(self, method_name, params): self.__lock.acquire() try: if not self.connection_ok(): self.open_connection() # marshal data from param tuple data = xmlrpclib.dumps(tuple([params]), method_name) #payload is 4 byte, little endian, field representing the length of #the xml data, followed by the data msg = struct.pack('<I', len(data)) + data try: self._s.send(msg) except: msglog.log('Adura', ERR, 'Error writing to XCommand socket.') raise EConnectionError rslt = self.read() finally: self.close_connection() self.__lock.release()
def handle_request(self, request): response = Response(request) response.set_header('Content-Type', 'text/xml') try: path = request.get_path().split('/') node_path = '/%s' % string.join(path[3:], '/') # # # # just for testing right now # # # if node_path == '' and len(path) < 4: results = self.invoke(request) xml = xmlrpclib.dumps((results, ), methodresponse=1) response.set_header('Content-Length', len(xml)) response.set_header('Content-Type', 'text/xml') response.send(xml) else: node = self._as_node(node_path) data = self._get_data(request) # process xmlrpc, getting the name of the method # and parameters params, method = xmlrpclib.loads(data) method_name = '' # get the name of the object # and the name of method m = getattr(node, method) if params == '': params = None result = (apply(m, params), ) if hasattr(result[0], 'has_key'): for k in result[0].keys(): if hasattr(result[0][k], 'has_key') \ and result[0][k].has_key('value') \ and isinstance(result[0][k]['value'],Exception): result[0][k][ 'value'] = 'error: %s' % result[0][k]['value'] xml = xmlrpclib.dumps(result, methodresponse=1) # XML-RPC Call was successful. # Send back the XML result to client response = Response(request) response.set_header('Content-Length', len(xml)) response.set_header('Content-Type', 'text/xml') response.send(xml) except Exception, err: if self.log_n_exceptions: msglog.exception() if self.log_n_exceptions > 0: self.log_n_exceptions -= 1 try: faultString = """<exception> <module>%s</module> <class>%s.%s</class> <str>%r</str> </exception>""" % (err.__class__.__module__, err.__class__.__module__, err.__class__.__name__, str(err)) except: msglog.exception() faultString = "%s" % err fault_xml = xmlrpclib.dumps(xmlrpclib.Fault(1, faultString)) response.set_header('Content-Length', len(fault_xml)) response.send(fault_xml)
def test_simple_conversions(self): for value, valid_result in self.SIMPLE_TESTS: result = dumps((value,)) self.assert_comparison(repr(result), '==', repr(valid_result)) return
def test_simple_conversions(self): for value, valid_result in self.SIMPLE_TESTS: result = dumps((value, )) self.assert_comparison(repr(result), '==', repr(valid_result)) return
def handle_request(self, request): response = Response(request) response.set_header('Content-Type', 'text/xml') try: path = request.get_path().split('/') node_path = '/%s' % string.join(path[3:],'/') # # # # just for testing right now # # # if node_path == '' and len(path) < 4: results = self.invoke(request) xml = xmlrpclib.dumps((results,),methodresponse=1) response.set_header('Content-Length', len(xml)) response.set_header('Content-Type', 'text/xml') response.send(xml) else: node = self._as_node(node_path) data = self._get_data(request) # process xmlrpc, getting the name of the method # and parameters params, method = xmlrpclib.loads(data) method_name = '' # get the name of the object # and the name of method m = getattr(node, method) if params == '': params = None result = (apply(m,params),) if hasattr(result[0],'has_key'): for k in result[0].keys(): if hasattr(result[0][k], 'has_key') \ and result[0][k].has_key('value') \ and isinstance(result[0][k]['value'],Exception): result[0][k]['value'] = 'error: %s' % result[0][k]['value'] xml = xmlrpclib.dumps(result,methodresponse=1) # XML-RPC Call was successful. # Send back the XML result to client response = Response(request) response.set_header('Content-Length', len(xml)) response.set_header('Content-Type', 'text/xml') response.send(xml) except Exception,err: if self.log_n_exceptions: msglog.exception() if self.log_n_exceptions > 0: self.log_n_exceptions -= 1 try: faultString = """<exception> <module>%s</module> <class>%s.%s</class> <str>%r</str> </exception>""" % (err.__class__.__module__, err.__class__.__module__, err.__class__.__name__, str(err)) except: msglog.exception() faultString = "%s" % err fault_xml = xmlrpclib.dumps( xmlrpclib.Fault(1, faultString) ) response.set_header('Content-Length', len(fault_xml)) response.send(fault_xml)