python - 为什么aiohttp的response里的cookies和程序出错时库里自动打印的cookies不一样?
迷茫
迷茫 2017-04-18 10:25:00
0
1
954

以下是获取cookies的一段代码,运行时是没有问题的。

    r = await session.post(url, data=postData)
    if r.status == 200:
        print ("Get Cookie Success!( Account:%s )" % account)
        cookie = r.cookies
        print (cookie)
        cookies.append(cookie)
    else:
        pass
    # 释放连接
    await r.release()

下图上边的红色框是上边代码的输出,如果程序中有bug,不管有什么bug时都会自动打印图中下边红色框的信息,上下两个红框中的cookies为什么不一样?post的url只有一个且返回的都是200。网上找了很久也找不到答案,望指教,谢谢!

迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

reply all(1)
刘奇

After reading the source code and locating it step by step, the problem was found. The first red box output was that there was a problem with the ClientResponse.cookies,第二个红框是出错时在ClientResponse.__del__经一番调用后在ClientResponse.__repr__输出的ClientResponse.headers
cookies是从headers提取的,使用的方法是http.cookies.SimpleCookie.load,最后发现是BaseCookie里的__parse_string method when using regular expression matching. The regular expression is as follows:

_CookiePattern = re.compile(r"""
    \s*                            # Optional whitespace at start of cookie
    (?P<key>                       # Start of group 'key'
    [""" + _LegalKeyChars + r"""]+?   # Any word of at least one letter
    )                              # End of group 'key'
    (                              # Optional group: there may not be a value.
    \s*=\s*                          # Equal Sign
    (?P<val>                         # Start of group 'val'
    "(?:[^\"]|\.)*"                  # Any doublequoted string
    |                                  # or
    \w{3},\s[\w\d\s-]{9,11}\s[\d:]{8}\sGMT  # Special case for "expires" attr
    |                                  # or
    [""" + _LegalValueChars + r"""]*      # Any word or empty string
    )                                # End of group 'val'
    )?                             # End of optional value group
    \s*                            # Any number of spaces.
    (\s+|;|$)                      # Ending either at space, semicolon, or EOS.
    """, re.ASCII | re.VERBOSE)    # re.ASCII may be removed if safe.

This paragraph is matchingexpires=Saturday, 06-Mar-2027 02:32:46 GMT;时第一次获取的keyexpires,valSaturday, ,第二次获取的key06-Mar-2027,valNone在函数里return了,因为不能正确解释expires所以造成cookiesheadersThe cookie information is different.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template