记录 Python 处理 cookie 时的一个 bug
2013 3 20 04:27 PM 2380次查看
			
			而 Tornado 使用 Cookie.SimpleCookie.load() 方法来解析 cookie:
class HTTPRequest(object):
    @property
    def cookies(self):
        """A dictionary of Cookie.Morsel objects."""
        if not hasattr(self, "_cookies"):
            self._cookies = Cookie.SimpleCookie()
            if "Cookie" in self.headers:
                try:
                    self._cookies.load(
                        native_str(self.headers["Cookie"]))
                except Exception:
                    self._cookies = {}
        return self._cookies而在用户尝试登录以后,虽然写入了正确的登录信息,却仍然无法被读取,于是就不可能成功登录了。
为了解决这个 bug,我只能修改 Cookie.BaseCookie.__ParseString() 的处理方式,让它忽略 Cookie.CookieError:
import Cookie
def __ParseString(self, str, patt=Cookie._CookiePattern):
    i = 0
    n = len(str)
    M = None
    while 0 <= i < n:
        match = patt.search(str, i)
        if not match: break
        K,V = match.group("key"), match.group("val")
        i = match.end(0)
        if K[0] == "$":
            if M:
                M[ K[1:] ] = V
        elif K.lower() in Cookie.Morsel._reserved:
            if M:
                M[ K ] = Cookie._unquote(V)
        else:
            rval, cval = self.value_decode(V)
            try:
                self._BaseCookie__set(K, rval, cval)
                M = self[K]
            except Cookie.CookieError:  # ignore
                M = None
Cookie.BaseCookie._BaseCookie__ParseString = __ParseString第二天,我去 Python 官网提交了一个 issue,不过似乎石沉大海了。所以还是记录在此吧。
 订阅
订阅
向下滚动可载入更多评论,或者点这里禁止自动加载。