0

i have many cookie strings that i get from a http response and save in a set. For example like this:

cookies = set()
cookies.add("__cfduid=123456789101112131415116; expires=Thu, 27-Aug-20 10:10:10 GMT; path=/; domain=.example.com; HttpOnly; Secure")
cookies.add("MUID=16151413121110987654321; domain=.bing.com; expires=Mon, 21-Sep-2020 10:10:11 GMT; path=/;, MUIDB=478534957198492834; path=/; httponly; expires=Mon, 21-Sep-2020 10:10:11 GMT")

Now i would like to parse that strings to an array or something else to access the data (domain, expires, ...) easier. For example like this:

cookie['MUID']['value']
cookie['MUID']['domain']
cookie['MUIDB']['path']
cookie['__cfduid']['Secure']
...

But i don't know how i do this. I try it with the SimpleCookie from http.cookies but i get not the expected result.

Basti
  • 1
  • 1

1 Answers1

0

You should create a python dictionary for this.

from collections import defaultdict
cookies = defaultdict(str)
list_of_strings = ["__cfduid=123456789101112131415116; expires=Thu, 27-Aug-20 10:10:10 GMT; path=/; domain=.example.com; HttpOnly; Secure"]# this is your list of strings you want to add
for string in list_of_strings:
    parts = string.split(";")
    for part in parts:
        temp = part.split("=")
        if len(temp) == 2:
            cookies[temp[0]] = temp[1]
Shan Ali
  • 564
  • 4
  • 12
  • Thanks but what is with attributes like`Secure` or `HttpOnly`? And what is with the strings that contains two or more single cookies like `MUID=16151413121110987654321; domain=.bing.com; expires=Mon, 21-Sep-2020 10:10:11 GMT; path=/;, MUIDB=478534957198492834; path=/; httponly; expires=Mon, 21-Sep-2020 10:10:11 GMT`? This contains the cookie `MUID` and `MUIDB` – Basti Aug 28 '19 at 16:56
  • did you use above code to run on sample cookies strings? I hope it will cover most of the cases – Shan Ali Aug 28 '19 at 16:59
  • If you want to keep `HttpOnly` or `Secure` then you can do this `cookies['HttpOnly'] = 1` and `cookies['Secure']=1` – Shan Ali Aug 28 '19 at 17:10
  • Now it try the code above and get an error `key, value = part.split("=") ValueError: not enough values to unpack (expected 2, got 1)` – Basti Aug 28 '19 at 17:27
  • Thank you very much for your effort! It gives me a [curious output](https://pastebin.com/raw/1S8bQDhB). I would like to access every attribute from a specific cookie (for example the cookie with the name `__cfduid`] like this: `cookies['__cfduid']['domain']` or for example the `path` like this: `cookies['__cfduid']['path']` – Basti Aug 28 '19 at 17:59