让python的cookie.py模块支持冒号做key的方法

为了做好兼容性,只能选择兼容:冒号。 很简单,修改一下cookie.morsel

代码如下:

#!/usr/bin/python # -*- coding: utf-8 -*- “””morselhook, fix cookie.cookieerror: illegal key value: ys-tab:entrance:e “”” import cookie import string _morsel = cookie.morsel class morselhook(_morsel): “”” >>> import inspect >>> (inspect.getargspec(morselhook.set)[3])[0] “abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789!#$%&’*+-.^_`|~:” >>> cookie = cookie.simplecookie() >>> cookie.load(“ys-tab:entrance:e=abc; webpy_session_) >>> print cookie set-cookie: webpy_session_”” 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) cookie.morsel = morselhook # 在你需要使用到cookie的地方先让上面的代码执行一遍 if __name__ == ‘__main__’: import doctest doctest.testmod()

Posted in 未分类

发表评论