如何让Cookie.py模块用冒号做key,cookie.py冒号,python的Cooki


python的Cookie.py模块本是不支持冒号做key的,那么要如何解决这个问题呢?虽然Cookie的标准是不允许:冒号出现在key里面的,还是任何难题也难不住我们的程序员们。 为了做好兼容性,只能选择兼容:冒号。 很简单的,修改一下Cookie.Morsel就可以了。

如何让Cookie.py模块用冒号做key,方法源码如下:(供参考)

#!/usr/bin/python# -*- coding: utf-8 -*-import Cookieimport string_Morsel = Cookie.Morselclass MorselHook(_Morsel):    """    >>> import inspect    >>> (inspect.getargspec(MorselHook.set)[3])[0]    "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!#$%&'*+-.^_`|~:"    >>> cookie = Cookie.SimpleCookie()    >>> cookie.load("ys-tab:entrance:e=abc; webpy_session_id=75eb60dcc83e2d902146af0bb7f47afe61fbd2b2")    >>> print cookie    Set-Cookie: webpy_session_id=75eb60dcc83e2d902146af0bb7f47afe61fbd2b2;    Set-Cookie: ys-tab:entrance:e=abc;    """    def set(self, key, val, coded_val, LegalChars=Cookie._LegalChars+':', idmap=string._idmap, translate=string.translate):        return super(MorselHook, self).set(key, val, coded_val, LegalChars, idmap, translate)#www.iplaypy.com    Cookie.Morsel = MorselHook# 在你需要使用到Cookie的地方先让上面的代码执行一遍if __name__ == '__main__':    import doctest    doctest.testmod()

编橙之家文章,

评论关闭