简体   繁体   English

使用蛇串行器进行安全的对象酸洗

[英]Using the serpent serializer for safe object pickling

As I understand it, using serpent is safer than pickle for serializing objects. 据我了解,使用蛇比腌制序列化对象更安全。

I use the following class: 我使用以下类:

import serpent

class Test:
    def save(self, fileName) :
        ser = serpent.dumps({"schema": self}, indent=True)
        open(fileName, "wb" ).write(ser)


    def load(self, fileName) :

        self = serpent.load(open(fileName, "rb"))["schema"]



    def someFunction(self) :

        [...]

I want to be able to do something like 我希望能够做类似的事情

test = Test()
test.save("afile")

[...]

test2 = Test().load()
test2.someFunction()

However, when I call Test().load() I get an object tree, not an object. 但是,当我调用Test()。load()时,我得到的是对象树,而不是对象。 So I cannot use it... How do I get the object from the object tree? 所以我不能使用它...如何从对象树中获取对象? Doing so, do I get back the exact same safety problem that I have with pickle? 这样做,我是否能获得与泡菜完全相同的安全性问题?

EDIT : From Pyro's doc : serpent serializes into Python literal expressions. 编辑:从Pyro的doc :蛇序列化为Python文字表达式。 Accepts quite a lot of different types. 接受很多不同的类型。 Many will be serialized as dicts. 许多将被序列化为字典。 You might need to explicitly translate literals back to specific types on the receiving end if so desired, because most custom classes aren't dealt with automatically. 如果需要的话,您可能需要在接收端将字面值显式转换回特定类型,因为大多数自定义类不会自动处理。

So, I guess in the end the questions is the following: Is there any recipes on how to translate such dictionary of literals back into object? 因此,我想最后是以下问题:是否有任何方法将这种文字词典转换回对象? I guess, it must has been made many times by many peoples... 我想,它一定是很多人做的很多次了。

There might be better answers to this question but since none has been proposed, I can share some pieces of solution from my own researches. 这个问题可能会有更好的答案,但是由于没有提出任何建议,我可以分享自己研究中的一些解决方案。 The solution to this problem depends very much on the structure of the classes that need to be serialized/deserialized but for an idea of how such a parsing can be made, one can have a look at the dict_to_class method implemented in Pyro4.util . 这个问题的解决方案取决于需要被序列化/反序列化,但对于这样的解析如何进行的想法,我们可以看看在实施dict_to_class方法的类的结构非常Pyro4.util

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM