Let the python cookie.py module support the colon as the key method

  • 2020-04-02 09:29:45
  • OfStack

For compatibility, you can only choose compatibility: colon.

That's easy. Change 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_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 

#  Before you need to use it Cookie Let the above code be executed first  


if __name__ == '__main__': 
import doctest 
doctest.testmod() 

Related articles: