文章目錄
雖然Cookie的標準是不允許:冒號出現在key裡面的,但是我們的開發人員是很可愛的,常常會讓我們意想不到。
為了做好相容性,只能選擇相容:冒號。
很簡單,修改一下Cookie.Morsel
#!/usr/bin/python# -*- coding: utf-8 -*-"""MorselHookfix Cookie.CookieError: Illegal key value: ys-tab:entrance:e"""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)Cookie.Morsel = MorselHook# 在你需要使用到Cookie的地方先讓上面的代碼執行一遍if __name__ == '__main__': import doctest doctest.testmod()
有愛
^_^ 希望本文對你有用。