import Cookie import string KOEKJE="__utma=96992031.2041186174.1349957326.1377589169.1377611835.32; __utmz=96992031.1349957326.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none);ti:mid=91F9A2B7-4254-4875-8C1D-F17B58ADB740;session_id_admin=127.0.0.1-e8e6c902-cf9a-4618-a335-e60795c8b9f8" #attempted minimal monkey patch, not working #Cookie._LegalChars+=":" print "Normal behaviour: skip rest of the keys, causing web2py to choke" print C = Cookie.SimpleCookie() try: C.load(KOEKJE) except Cookie.CookieError, e: print "problem %s"%e pass print C.output() def mp_set(self, key, val, coded_val, LegalChars=Cookie._LegalChars+":", idmap=Cookie._idmap, translate=string.translate): # First we verify that the key isn't a reserved word # Second we make sure it only contains legal characters if key.lower() in self._reserved: raise Cookie.CookieError("Attempt to set a reserved key: %s" % key) if "" != translate(key, idmap, LegalChars): raise Cookie.CookieError("Illegal key value: %s" % key) # It's a good key, so save it. self.key = key self.value = val self.coded_value = coded_val # end set # monkeypatch: # just replace set function Cookie.Morsel.set=mp_set print print "Changed behaviour using monkey patch:" print D = Cookie.SimpleCookie() try: D.load(KOEKJE) except Cookie.CookieError, e: print "problem %s"%e pass print D.output() # new Morsel class class Morsel1(Cookie.Morsel): def __init__(self): super(Morsel1,self).__init__() def set(self, key, val, coded_val, LegalChars=Cookie._LegalChars+":", idmap=Cookie._idmap, translate=string.translate): # First we verify that the key isn't a reserved word # Second we make sure it only contains legal characters if key.lower() in self._reserved: raise Cookie.CookieError("Attempt to set a reserved key: %s" % key) if "" != translate(key, idmap, LegalChars): raise Cookie.CookieError("Illegal key value: %s" % key) # It's a good key, so save it. self.key = key self.value = val self.coded_value = coded_val # end set # new Cookieclass class BrowserCookie(Cookie.SimpleCookie): def __init__(self): super(BrowserCookie,self).__init__() def __set(self, key, real_value, coded_value): """Private method for setting a cookie's value""" M = self.get(key, Morsel1()) M.set(key, real_value, coded_value) dict.__setitem__(self, key, M) # end __set def __ParseString(self, strg, patt=Cookie._CookiePattern): i = 0 # Our starting point n = len(strg) # Length of string M = None # current morsel while 0 <= i < n: # Start looking for a cookie match = patt.search(strg, i) if not match: break # No more cookies K,V = match.group("key"), match.group("val") i = match.end(0) # Parse the key, value in case it's metainfo if K[0] == "$": # We ignore attributes which pertain to the cookie # mechanism as a whole. See RFC 2109. # (Does anyone care?) if M: M[ K[1:] ] = V elif K.lower() in Morsel1._reserved: if M: M[ K ] = Cookie._unquote(V) else: rval, cval = self.value_decode(V) self.__set(K, rval, cval) M = self[K] # end __ParseString print print "Changed behaviour using subclassing" print E = BrowserCookie() try: E.load(KOEKJE) except Cookie.CookieError, e: print "problem %s"%e pass print E.output()
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question