13

How to convert the response['set-cookie'] output string from httplib2 response like

"cookie1=xxxyyyzzz;Path=/;Expires=Wed, 03-Feb-2015 08:03:12 GMT;Secure;HttpOnly, cookie2=abcdef;Path=/;Secure"

to

{'cookie1':'xxxyyyzzz','cookies2':'abcdef'}
ospider
  • 9,334
  • 3
  • 46
  • 46
Ahmed Daif
  • 379
  • 3
  • 12
  • What have you tried? Seems like a simple case of splitting a string on separators, moreover [the `set-cookie` header syntax](http://tools.ietf.org/search/rfc6265#page-8) does not allow separators within the cookie name _or_ value, so you wouldn't even need to bother with parsing quoting constructs. – lanzz Feb 03 '14 at 08:27
  • If you used requests, you could just use `response.cookies` and get back a dictionary. – Blender Feb 03 '14 at 08:28

3 Answers3

15

Use http.cookies:

>>> c = "cookie1=xxxyyyzzz;Path=/;Expires=Wed, 03-Feb-2015 08:03:12 GMT;Secure;HttpOnly, cookie2=abcdef;Path=/;Secure"
>>> from http.cookies import SimpleCookie
>>> cookie = SimpleCookie()
>>> cookie.load(c)
>>> cookie
<SimpleCookie: cookie1='xxxyyyzzz' cookie2='abcdef'>
>>> {key: value.value  for key, value in cookie.items()}
{'cookie1': 'xxxyyyzzz', 'cookie2': 'abcdef'}
falsetru
  • 357,413
  • 63
  • 732
  • 636
  • 1
    Excellent. This really should be a method `as_dict` on the BaseCookie class! – SleepyCal Apr 28 '20 at 16:14
  • Sorry, I don't know why , need some help: Python 3.7.6 (default, Dec 30 2019, 19:38:26) [Clang 11.0.0 (clang-1100.0.33.16)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> `c = "cookie1=xxxyyyzzz;Path=/;Expires=Wed, 03-Feb-2015 08:03:12 GMT;Secure;HttpOnly, cookie2=abcdef;Path=/;Secure"` >>> `from http.cookies import SimpleCookie` >>> `cookie = SimpleCookie()` >>> `cookie.load(c)` >>> `cookie` `` >>> `cookie.items()` `dict_items([])` >>> – RickyChan Jan 09 '23 at 02:02
  • @RickyChan, How about post a separated question instead of commenting here? Seems some question text are truncated. BTW, you're trying to parse expired http-only cookie. – falsetru Jan 09 '23 at 06:18
  • This doesn't work using **Python 3.10.0**, the generated cookie is empty (it does work with a single cookie, but not with string composed of two cookies). – cglacet Feb 21 '23 at 18:32
  • Maybe its related to iOS because it works within docker, strange. – cglacet Feb 21 '23 at 19:04
  • @cglacet, If I change `expires` date and remove `Secure`, `HttpOnly`, It still works. (tested with Python 3.11.2, in Linux). – falsetru Feb 22 '23 at 08:19
  • Yes but it should work with everything on, its a bit strange but the documentation is not even clear on whether it is intended to work on multiple entries or not. – cglacet Feb 22 '23 at 17:27
7
def parse_dict_cookies(cookies):
    result = {}
    for item in cookies.split(';'):
        item = item.strip()
        if not item:
            continue
        if '=' not in item:
            result[item] = None
            continue
        name, value = item.split('=', 1)
        result[name] = value
    return result
nezort11
  • 326
  • 1
  • 4
  • 11
wcc526
  • 3,915
  • 2
  • 31
  • 29
-2

if you are using requests, response.cookies is dict-like object

CS QGB
  • 297
  • 1
  • 3
  • 12