【问题标题】:XML-RPC - cannot marshal recursive dictionariesXML-RPC - 无法编组递归字典
【发布时间】:2011-08-31 07:02:18
【问题描述】:

我有一个通过 xml-rpc 发送字典的简单示例:

 class CTest(object):
    def __init__(self):
        self.node1 = {'data':'zek', 'parent':{},  'children':[]}
        self.node2 = {'data':'bill', 'parent':{}, 'children':[]}
        self.node1['children'].append(self.node2)
        self.node2['parent'] = self.node1

    def getNode(self):
        return self.node1

我有两个字典:node2 是 node1 的孩子,同时 node2 有 node1 的引用作为父变量。 所以它是一个递归字典。当我尝试通过 XML-RPC 发送 node1 时,我得到了这个异常:

#Command to execute xml-rpc dump method for serialization
test = CTest()
xmlrpclib.dumps((test,), 'Node Object')
#Exception
raise TypeError, "cannot marshal recursive dictionaries"

是否可以通过 XML-RPC 发送 node1(不改变字典结构)?

谢谢。

【问题讨论】:

    标签: python recursion dictionary marshalling xml-rpc


    【解决方案1】:

    使用 Python 的“pickle”模块自行序列化和反序列化“测试”。

    cPickle.dumps(test)
    

    正在工作。在你使用的电线的另一边

    cPickle.loads(received_test_pickle)
    

    可能需要在 XMLRPC 调用之前/之后对 pickle 进行 base-64 编码/解码。

    但也要研究 PyRo

    http://pyro.sourceforge.net/

    【讨论】:

    • 谢谢。它与 cPickle 完美搭配。但它需要与语言无关。因此,当我在一个对等方上腌制字典时,我还需要在其他对等方中使用 python 来解压它。因为我以 xml 格式发送字典,所以通常可以在另一个对等方(C、C++、java)中使用另一种编程语言,这就是为什么我不能使用 pickle。 Pyro 有自己的网络通信代码,它不使用 XML-RPC。我需要使用 XML-RPC。那么你知道通过 XML-RPC 发送这个字典而不用 pickle 的其他方法吗?
    猜你喜欢
    • 2019-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-10
    • 2012-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多