def test_xml_loads(): """ Test the `ipalib.rpc.xml_loads` function. """ f = rpc.xml_loads params = (binary_bytes, utf8_bytes, unicode_str, None) wrapped = rpc.xml_wrap(params, API_VERSION) # Test un-serializing an RPC request: data = dumps(wrapped, 'the_method', allow_none=True) (p, m) = f(data) assert_equal(m, u'the_method') assert_equal(p, params) # Test un-serializing an RPC response: data = dumps((wrapped,), methodresponse=True, allow_none=True) (tup, m) = f(data) assert m is None assert len(tup) == 1 assert type(tup) is tuple assert_equal(tup[0], params) # Test un-serializing an RPC response containing a Fault: for error in (unicode_str, u'hello'): fault = Fault(69, error) data = dumps(fault, methodresponse=True, allow_none=True, encoding='UTF-8') e = raises(Fault, f, data) assert e.faultCode == 69 assert_equal(e.faultString, error) assert type(e.faultString) is unicode
def _marshaled_dispatch(self, request, dispatch_method=None): """Dispatches an XML-RPC method from marshalled (XML) data. XML-RPC methods are dispatched from the marshalled (XML) data using the _dispatch method and the result is returned as marshalled data. For backwards compatibility, a dispatch function can be provided as an argument (see comment in SimpleXMLRPCRequestHandler.do_POST) but overriding the existing method through subclassing is the prefered means of changing method dispatch behavior. """ data = request.body params, method = xmlrpclib.loads(data) # add request to params params = (request, ) + params # generate response try: if dispatch_method is not None: response = dispatch_method(method, params) else: response = self._dispatch(method, params) # wrap response in a singleton tuple response = (response, ) response = xmlrpclib.dumps(response, methodresponse=1, allow_none=self.allow_none, encoding=self.encoding) except xmlrpclib.Fault as fault: response = xmlrpclib.dumps(fault, allow_none=self.allow_none, encoding=self.encoding) except: # report exception back to server if settings.DEBUG: from kobo.tback import Traceback response = xmlrpclib.dumps(xmlrpclib.Fault( 1, u"%s" % Traceback().get_traceback()), allow_none=self.allow_none, encoding=self.encoding) else: exc_info = sys.exc_info()[1] exc_type = exc_info.__class__.__name__ response = xmlrpclib.dumps(xmlrpclib.Fault( 1, "%s: %s" % (exc_type, exc_info)), allow_none=self.allow_none, encoding=self.encoding) return response
def listresources(url, root_bundle, cert, key, cred_strings, options=None, sliceurn=None): if not options: options = {} opts = { "geni_rspec_version": { "version": "3", "type": "GENI" }, "geni_available": False, "geni_compressed": False } if sliceurn: opts["geni_slice_urn"] = sliceurn # Allow all options to be overridden by the caller opts.update(options) req_data = xmlrpclib.dumps((cred_strings, opts), methodname="ListResources") return _rpcpost(url, req_data, (cert, key), root_bundle)
def GetCredential(url, root_bundle, cert, key, urn=None, uuid=None): if urn: req_data = xmlrpclib.dumps(({ "urn": urn, "type": "Slice" }, ), methodname="GetCredential") elif uuid: req_data = xmlrpclib.dumps(({ "uuid": uuid, "type": "Slice" }, ), methodname="GetCredential") else: req_data = xmlrpclib.dumps(tuple(), methodname="GetCredential") return _rpcpost(url, req_data, (cert, key), root_bundle)
def lookup_project_members(url, root_bundle, cert, key, cred_strings, project_urn): options = {} req_data = xmlrpclib.dumps(("PROJECT", project_urn, cred_strings, options), methodname="lookup_members") return _rpcpost(url, req_data, (cert, key), root_bundle)
def modify_project_membership(url, root_bundle, cert, key, cred_strings, project_urn, add=None, remove=None, change=None): options = {} if add: to_add = [] for urn, role in add: to_add.append({"PROJECT_MEMBER": urn, "PROJECT_ROLE": role}) options["members_to_add"] = to_add if remove: options["members_to_remove"] = remove if change: to_change = [] for urn, role in change: to_change.append({"PROJECT_MEMBER": urn, "PROJECT_ROLE": role}) options["members_to_change"] = to_change req_data = xmlrpclib.dumps(("PROJECT", project_urn, cred_strings, options), methodname="modify_membership") return _rpcpost(url, req_data, (cert, key), root_bundle)
def validate_xmlrpc(data): """ Raise an error if the data can't be dumped to XMLRPC format. Given that the Supervisor RPC interface can return any proc's config, and that some things (like >32 bit ints, or integer keys in dicts) can't be marshalled as XML RPC, this check protects the system from problems deployed under Supervisor D. """ try: xmlrpc_client.dumps((data, ), allow_none=True) except Exception as e: tmpl = "Cannot be marshalled to XMLRPC: %s" raise django.core.exceptions.ValidationError(tmpl % e) return data
def _test_request(self, **kwargs): r = self.request_class('http://scrapytest.org/rpc2', **kwargs) self.assertEqual(r.headers[b'Content-Type'], b'text/xml') self.assertEqual(r.body, to_bytes(xmlrpclib.dumps(**kwargs))) self.assertEqual(r.method, 'POST') self.assertEqual(r.encoding, kwargs.get('encoding', 'utf-8')) self.assertTrue(r.dont_filter, True)
def test_no_i8(self): # we shouldn't use i8 if we don't have to data = [ 23, 42, -1024, 2**31 - 1, -2**31, [2**31 - 1], { "a": -2**31, "b": 3.14 }, ] for value in data: value = (value, ) enc = xmlrpcplus.dumps(value, methodresponse=1, encoding='us-ascii') _enc = xmlrpc_client.dumps(value, methodresponse=1, allow_none=1, encoding='us-ascii') if 'i8' in enc or 'I8' in enc: raise Exception('i8 used unnecessarily') self.assertEqual(enc, _enc) params, method = xmlrpc_client.loads(enc) self.assertEqual(params, value) self.assertEqual(method, None)
def create_request(url, root_bundle, cert, key, cred_strings, project_id, desc): JOIN = 0 DUMMY_ATTRS = "" req_data = xmlrpclib.dumps((REQCTX.PROJECT, project_id, JOIN, desc, DUMMY_ATTRS, cred_strings, {}), methodname="create_request") return _rpcpost(url, req_data, (cert, key), root_bundle)
def test_response(self): for value in self.standard_data: value = (value, ) enc = xmlrpcplus.dumps(value, methodresponse=1) _enc = xmlrpc_client.dumps(value, methodresponse=1, allow_none=1) self.assertEqual(enc, _enc) params, method = xmlrpc_client.loads(enc) self.assertEqual(params, value) self.assertEqual(method, None)
def test_response(self): for value in self.standard_data: value = (value,) enc = xmlrpcplus.dumps(value, methodresponse=1) _enc = xmlrpc_client.dumps(value, methodresponse=1, allow_none=1) self.assertEqual(enc, _enc) params, method = xmlrpc_client.loads(enc) self.assertEqual(params, value) self.assertEqual(method, None)
def _marshaled_dispatch(self, request, dispatch_method = None): """Dispatches an XML-RPC method from marshalled (XML) data. XML-RPC methods are dispatched from the marshalled (XML) data using the _dispatch method and the result is returned as marshalled data. For backwards compatibility, a dispatch function can be provided as an argument (see comment in SimpleXMLRPCRequestHandler.do_POST) but overriding the existing method through subclassing is the prefered means of changing method dispatch behavior. """ data = request.body params, method = xmlrpclib.loads(data) # add request to params params = (request, ) + params # generate response try: if dispatch_method is not None: response = dispatch_method(method, params) else: response = self._dispatch(method, params) # wrap response in a singleton tuple response = (response,) response = xmlrpclib.dumps(response, methodresponse=1, allow_none=self.allow_none, encoding=self.encoding) except xmlrpclib.Fault as fault: response = xmlrpclib.dumps(fault, allow_none=self.allow_none, encoding=self.encoding) except: # report exception back to server if settings.DEBUG: from kobo.tback import Traceback response = xmlrpclib.dumps( xmlrpclib.Fault(1, u"%s" % Traceback().get_traceback()), allow_none=self.allow_none, encoding=self.encoding) else: response = xmlrpclib.dumps( xmlrpclib.Fault(1, "%s: %s" % (sys.exc_type.__name__, sys.exc_info()[1])), allow_none=self.allow_none, encoding=self.encoding) return response
def delete_project(url, root_bundle, cert, key, cred_strings, project_urn): """Delete project by URN .. note:: You may or may not be able to delete projects as a matter of policy for the given authority.""" options = {} req_data = xmlrpclib.dumps(("PROJECT", project_urn, cred_strings, options), methodname="delete") return _rpcpost(url, req_data, (cert, key), root_bundle)
def test_call(self): method = 'my_rpc_method' for value in self.standard_data: value = (value, "other arg") enc = xmlrpcplus.dumps(value, methodname=method) _enc = xmlrpc_client.dumps(value, methodname=method, allow_none=1) self.assertEqual(enc, _enc) params, method = xmlrpc_client.loads(enc) self.assertEqual(params, value) self.assertEqual(method, method)
def send(self, method, params): request = dumps(params, method) try: response = self.transport.request("localhost", "/RPC2", request) except Fault as e: response = e.faultString if isinstance(response, tuple): response = response[0] return response
def listimages(url, root_bundle, cert, key, cred_strings, owner_urn, options=None): if not options: options = {} req_data = xmlrpclib.dumps((owner_urn, cred_strings, options), methodname="ListImages") return _rpcpost(url, req_data, (cert, key), root_bundle)
def test_just_data(self): # xmlrpc_client supports this case, so I guess we should too # neither method call nor response for value in self.standard_data: value = (value, "foo", "bar") enc = xmlrpcplus.dumps(value) _enc = xmlrpc_client.dumps(value, allow_none=1) self.assertEqual(enc, _enc) params, method = xmlrpc_client.loads(enc) self.assertEqual(params, value) self.assertEqual(method, None)
def delete (url, root_bundle, cert, key, creds, urns, options = None): if not options: options = {} if not isinstance(urns, list): urns = [urns] cred_list = [] for cred in creds: cred_list.append({"geni_value" : open(cred.path, "rb").read(), "geni_type" : cred.type, "geni_version" : cred.version}) req_data = xmlrpclib.dumps((urns, cred_list, options), methodname="Delete") return _rpcpost(url, req_data, (cert, key), root_bundle)
def allocate (url, root_bundle, cert, key, creds, slice_urn, rspec, options = None): if not options: options = {} cred_list = [] for cred in creds: cred_list.append({"geni_value" : open(cred.path, "rb").read(), "geni_type" : cred.type, "geni_version" : cred.version}) req_data = xmlrpclib.dumps((slice_urn, cred_list, rspec, options), methodname="Allocate") return _rpcpost(url, req_data, (cert, key), root_bundle)
def serialize(self, operation, shape, params): args = [] if shape: for member in shape.iter_members(): if member.destination == 'body': args.append(params[member.name]) return xmlrpc_client.dumps( tuple(args), operation.wire_name, allow_none=True, )
def renewsliver(url, root_bundle, cert, key, creds, slice_urn, date, options=None): FMT = "%Y-%m-%dT%H:%M:%S+00:00" if not options: options = {} req_data = xmlrpclib.dumps((slice_urn, creds, date.strftime(FMT), options), methodname="RenewSliver") return _rpcpost(url, req_data, (cert, key), root_bundle)
def createsliver(url, root_bundle, cert, key, creds, slice_urn, rspec, users, options=None): if not options: options = {} req_data = xmlrpclib.dumps((slice_urn, creds, rspec, users, options), methodname="CreateSliver") return _rpcpost(url, req_data, (cert, key), root_bundle)
def test_encoding(self): data = [ 45, ["hello", "world"], {"a": 5.5, "b": [None]}, ] for value in data: value = (value,) enc = xmlrpcplus.dumps(value, methodresponse=1, encoding='us-ascii') _enc = xmlrpc_client.dumps(value, methodresponse=1, allow_none=1, encoding='us-ascii') self.assertEqual(enc, _enc) params, method = xmlrpc_client.loads(enc) self.assertEqual(params, value) self.assertEqual(method, None)
def rpc_pypi(method, *args): """Call an XML-RPC method on the Pypi server.""" conn = http_client.HTTPSConnection(PYPI_HOSTNAME) headers = {'Content-Type': 'text/xml'} payload = xmlrpc_client.dumps(args, method) conn.request("POST", "/pypi", payload, headers) response = conn.getresponse() if response.status == 200: result = xmlrpc_client.loads(response.read())[0][0] return result else: raise RuntimeError("Unable to download the list of top " "packages from Pypi.")
def test_fault(self): code = 1001 msg = "some useless error" f1 = xmlrpcplus.Fault(code, msg) f2 = xmlrpc_client.Fault(code, msg) value = f1 enc = xmlrpcplus.dumps(value, methodresponse=1) _enc = xmlrpc_client.dumps(value, methodresponse=1, allow_none=1) self.assertEqual(enc, _enc) try: params, method = xmlrpc_client.loads(enc) except xmlrpc_client.Fault as e: self.assertEqual(e.faultCode, code) self.assertEqual(e.faultString, msg) else: raise Exception('Fault not raised')
def __init__(self, *args, **kwargs): encoding = kwargs.get('encoding', None) if 'body' not in kwargs and 'params' in kwargs: kw = dict((k, kwargs.pop(k)) for k in DUMPS_ARGS if k in kwargs) kwargs['body'] = xmlrpclib.dumps(**kw) # spec defines that requests must use POST method kwargs.setdefault('method', 'POST') # xmlrpc query multiples times over the same url kwargs.setdefault('dont_filter', True) # restore encoding if encoding is not None: kwargs['encoding'] = encoding super(XmlRpcRequest, self).__init__(*args, **kwargs) self.headers.setdefault('Content-Type', 'text/xml')
def lookup_projects_for_member(url, root_bundle, cert, key, cred_strings, member_urn, expired=None): options = {} match = {} if expired is not None: match["PROJECT_EXPIRED"] = expired if match: options["match"] = match req_data = xmlrpclib.dumps(("PROJECT", member_urn, cred_strings, options), methodname="lookup_for_member") return _rpcpost(url, req_data, (cert, key), root_bundle)
def create_project(url, root_bundle, cert, key, cred_strings, name, exp, desc=None): fields = {} fields["PROJECT_EXPIRATION"] = exp.strftime(DATE_FMT) fields["PROJECT_NAME"] = name if desc is not None: fields["PROJECT_DESCRIPTION"] = desc req_data = xmlrpclib.dumps(("PROJECT", cred_strings, { "fields": fields }), methodname="create") return _rpcpost(url, req_data, (cert, key), root_bundle)
def create_slice(url, root_bundle, cert, key, cred_strings, name, proj_urn, exp=None, desc=None): fields = {} fields["SLICE_NAME"] = name if proj_urn: fields["SLICE_PROJECT_URN"] = proj_urn if exp: fields["SLICE_EXPIRATION"] = exp.strftime(DATE_FMT) if desc: fields["SLICE_DESCRIPTION"] = desc req_data = xmlrpclib.dumps(("SLICE", cred_strings, { "fields": fields }), methodname="create") return _rpcpost(url, req_data, (cert, key), root_bundle)
def test_no_i8(self): # we shouldn't use i8 if we don't have to data = [ 23, 42, -1024, 2 ** 31 - 1, -2 ** 31, [2**31 -1], {"a": -2 ** 31, "b": 3.14}, ] for value in data: value = (value,) enc = xmlrpcplus.dumps(value, methodresponse=1, encoding='us-ascii') _enc = xmlrpc_client.dumps(value, methodresponse=1, allow_none=1, encoding='us-ascii') if 'i8' in enc or 'I8' in enc: raise Exception('i8 used unnecessarily') self.assertEqual(enc, _enc) params, method = xmlrpc_client.loads(enc) self.assertEqual(params, value) self.assertEqual(method, None)
def scgi_request(url, methodname, *params, **kw): """ Send a XMLRPC request over SCGI to the given URL. @param url: Endpoint URL. @param methodname: XMLRPC method name. @param params: Tuple of simple python objects. @keyword deserialize: Parse XML result? (default is True) @return: XMLRPC response, or the equivalent Python data. """ xmlreq = xmlrpclib.dumps(params, methodname) xmlresp = SCGIRequest(url).send(xmlreq) if kw.get("deserialize", True): # This fixes a bug with the Python xmlrpclib module # (has no handler for <i8> in some versions) xmlresp = xmlresp.replace("<i8>", "<i4>").replace("</i8>", "</i4>") # Return deserialized data return xmlrpclib.loads(xmlresp)[0][0] else: # Return raw XML return xmlresp
def testBigNumbersInStats(self): host_monitor = mock.Mock() host_monitor.interrogate.return_value.statistics = [{ "ksm_run": 1, "ksm_pages": 100, "huge_number": 2**31 + 2**10 }] vm1 = mock.Mock() vm1.properties = {"name": "vm1"} vm1.statistics = [{ "free_mem": 25 * 2**30 # 25 TiB (in KiB) }] vm2 = mock.Mock() vm2.properties = {"name": "vm2"} vm2.statistics = [{ "free_mem": 30 * 2**20 # 30 GiB (in KiB) }] guest_manager = mock.Mock() guest_manager.interrogate.return_value.values.return_value = [ vm1, vm2 ] threads = { "host_monitor": host_monitor, "guest_manager": guest_manager } funcs = mom.MOMFuncs(None, threads) data = funcs.getStatistics() mom.enable_i8() packet = xmlrpc_client.dumps((data,)) (reply,), func = xmlrpc_client.loads(packet) self.assertEqual(data, reply)
def scgi_request(url, methodname, *params, **kw): """ Send a XMLRPC request over SCGI to the given URL. :param url: Endpoint URL. :param methodname: XMLRPC method name. :param params: Tuple of simple python objects. :type url: string :type methodname: string :keyword deserialize: Parse XML result? (default is True) :return: XMLRPC string response, or the equivalent Python data. """ xmlreq = xmlrpclib.dumps(params, methodname) xmlresp = SCGIRequest(url).send(xmlreq.encode()).decode() if kw.get("deserialize", True): # This fixes a bug with the Python xmlrpclib module # (has no handler for <i8> in some versions) xmlresp = xmlresp.replace("<i8>", "<i4>").replace("</i8>", "</i4>") # Return deserialized data return xmlrpclib.loads(xmlresp)[0][0] else: # Return raw XML return xmlresp
def dump_n_load(value): (param, method) = loads( dumps((value,), allow_none=True) ) return param[0]
def dump_n_load(value): param, _method = loads( dumps((value,), allow_none=True) ) return param[0]