【问题标题】:Pylons & Beaker: JSON Encoded SessionsPylons & Beaker:JSON 编码会话
【发布时间】:2011-08-16 02:39:32
【问题描述】:

需要在 node.js 中读取 Pylons 会话数据(只是读取,而不是写入)

解码 base64 后,我会得到一个包含序列化 Python 对象的字符串,在 node.js 中解析该对象很痛苦

如何让 Beaker 序列化为 JSON?因为 node.js 处理起来要容易得多。

【问题讨论】:

    标签: python node.js pylons beaker


    【解决方案1】:

    我不得不在烧杯里面找到你所谓的“Python 序列化字符串”是 python 泡菜。

    我认为改变它不会超过几行,让它使用 json 来存储字典。

    这是针对https://bitbucket.org/bbangert/beaker/src/257f147861c8的补丁:

    diff -r 257f147861c8 beaker/session.py
    --- a/beaker/session.py Mon Apr 18 11:38:53 2011 -0400
    +++ b/beaker/session.py Sat Apr 30 14:19:12 2011 -0400
    @@ -489,10 +489,10 @@
                 nonce = b64encode(os.urandom(40))[:8]
                 encrypt_key = crypto.generateCryptoKeys(self.encrypt_key,
                                                  self.validate_key + nonce, 1)
    -            data = util.pickle.dumps(self.copy(), 2)
    +            data = util.json.dumps(self.copy())
                 return nonce + b64encode(crypto.aesEncrypt(data, encrypt_key))
             else:
    -            data = util.pickle.dumps(self.copy(), 2)
    +            data = util.json.dumps(self.copy())
                 return b64encode(data)
    
         def _decrypt_data(self):
    @@ -504,10 +504,10 @@
                                                  self.validate_key + nonce, 1)
                 payload = b64decode(self.cookie[self.key].value[8:])
                 data = crypto.aesDecrypt(payload, encrypt_key)
    -            return util.pickle.loads(data)
    +            return util.json.loads(data)
             else:
                 data = b64decode(self.cookie[self.key].value)
    -            return util.pickle.loads(data)
    +            return util.json.loads(data)
    
         def save(self, accessed_only=False):
             """Saves the data for this session to persistent storage"""
    diff -r 257f147861c8 beaker/util.py
    --- a/beaker/util.py    Mon Apr 18 11:38:53 2011 -0400
    +++ b/beaker/util.py    Sat Apr 30 14:19:12 2011 -0400
    @@ -24,6 +24,11 @@
         import pickle
     else:
         import cPickle as pickle
    +
    +try:
    +    import json
    +except ImportError:
    +    import simplejson as json
    
     from beaker.converters import asbool
     from beaker import exceptions
    

    【讨论】:

    • +1 用于进入源代码、修复问题并推送补丁。
    猜你喜欢
    • 2011-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-02
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    • 1970-01-01
    相关资源
    最近更新 更多