Converting cookie string into Python dict

前端 未结 2 1626
耶瑟儿~
耶瑟儿~ 2021-02-19 22:24

In Fiddler, I captured an HTTPS request with the following cookie string sent from the client (visible in Inspectors > Raw):

Cookie: devicePixelRatio=1; ident=ex         


        
2条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-19 22:36

    Try this function, definitly it will help :). This function supports more cookie attributes like comment, priority, version and Max-Age

    def Robotcookie(cookie: str, parent_domain: str):
        items = cookie.split(';')
        SameSite = HttpOnly = Secure = Domain = Path = Expires = Comment = MaxAge = CookieName = CookieValue = Size = Sessionkey = Version = Priority = None
        CookieName = CookieValue = None
        idx = len(items) - 1
        while idx >= 0:
            item = items[idx].strip()
            idx -= 1
            if not item:
                continue
            SameSiteMatched = re.match(r'^SameSite(.*)?', item, re.I)
            HttpOnlyMatched = SameSiteMatched or re.match(r'^HttpOnly(.*)$', item, re.I)
            SecureMatched = HttpOnlyMatched or re.match(r'^Secure(.*)$', item, re.I)
            DomainMatched = SecureMatched or re.match(r'^Domain(.*)?', item, re.I)
            PathMatched = DomainMatched or re.match(r'^Path(.*)?', item, re.I)
            ExpiresMatched = PathMatched or re.match(r'^Expires(.*)?', item, re.I)
            CommentMatched = ExpiresMatched or re.match(r'^Comment(.*)?', item, re.I)
            MaxAgeMatched = ExpiresMatched or re.match(r'^Max-Age=(.*)?', item, re.I)
            VersionMatched = MaxAgeMatched or re.match(r'^Version=(.*)?', item, re.I)
            PriorityMatched = VersionMatched or re.match(r'^priority=(.*)?', item, re.I)
            matched = SameSiteMatched or HttpOnlyMatched or SecureMatched or DomainMatched or PathMatched or ExpiresMatched or CommentMatched or MaxAgeMatched or VersionMatched or PriorityMatched
            if matched:
                val = matched.groups(0)[0].lstrip('=')
                if matched == SameSiteMatched:
                    SameSite = val if val.lower() in ['strict', 'lax', 'none'] else None
                elif matched == HttpOnlyMatched:
                    HttpOnly = True
                elif matched == SecureMatched:
                    Secure = True
                elif matched == DomainMatched:
                    Domain = val
                elif matched == PathMatched:
                    Path = val
                elif matched == PathMatched:
                    Path = val
                elif matched == ExpiresMatched:
                    Expires = val
                elif matched == CommentMatched:
                    Comment = val
                elif matched == MaxAgeMatched:
                    MaxAge = val
                elif matched == VersionMatched:
                    Version = val
                elif matched == PriorityMatched:
                    Priority = val
            else:
                CookieMatched = re.match(r'^(.[^=]*)=(.*)?', item, re.I)
                if CookieMatched:
                    CookieName, CookieValue = CookieMatched.groups(0)
    
        Sessionkey = True if not Expires else False
        Size = (len(CookieName) if CookieName else 0) + (len(CookieValue) if CookieValue else 0)
    
        Domain = parent_domain if CookieName and not Domain else Domain
        Path = '/' if CookieName and not Path else Path
        Priority = 'Medium' if CookieName and not Priority else Priority.title() if Priority else 'Medium'
    
        Cookie = {
            CookieName: CookieValue,
            'Domain': Domain,
            'Path': Path,
            'Expires': Expires,
            'Comment': Comment,
            'MaxAge': MaxAge,
            'SameSite': SameSite,
            'HttpOnly': HttpOnly,
            'Secure': Secure,
            'Size': Size,
            'Sessionkey': Sessionkey,
            'Version': Version,
            'Priority': Priority
        }
        return Cookie if CookieName else None
        
    
    cookie = 'name=bijaya; comment=Comment1; expires=Mon, 26-Jul-2021 06:34:02 GMT; path=/; domain=.google.com; Secure; HttpOnly; SameSite=none; Max-Age=244114; Version=1.2; priority=high;'
    CookieDict = Robotcookie(cookie, parent_domain='google.com')
    {'name': 'bijaya', 'Domain': '.google.com', 'Path': '/', 'Expires': None, 'Comment': 'Comment1', 'MaxAge': '244114', 'SameSite': 'none', 'HttpOnly': True, 'Secure': True, 'Size': 179, 'Sessionkey': True, 'Version': '1.2', 'Priority': 'High'}
    

提交回复
热议问题