Kryštof Hilar
Kryštof Hilar

Reputation: 639

Python parsing set-cookie header

In PHP I send one cookie with secure and http only flags, and other without

setcookie("c2","value");
setcookie("c1","value", 0, "/", "", true, true);

It produces header

Set-Cookie: c2=value, c1=value; path=/; secure; httponly

In firebug I can see, this is OK (c1 secure flag is True, c2 is False)

I want to get which one of them is not using secure flag My python code:

cookies = Cookie.SimpleCookie()
cookies.load(headers['set-cookie'])
print cookies

Output:
Set-Cookie: c1=value; Path=/\\r\\nSet-Cookie: c2=value

headers['set-cookie'] does contain original set-cookie header, it's ok According to python documentation printing(handling as string) SimpleCookie instance should create set-cookie header. Point is, that something is missing after parsing original header. Morsels also contains wrong values (secure and http only).

Is this some kind of misconfiguration or it's a bug in python library ? Thanks :)

Upvotes: 3

Views: 6824

Answers (1)

Riley
Riley

Reputation: 4822

This might be a bit late but saw your question and thought you may still need help. The code I use to read a cookie is:

import Cookie,os
def getCookieData():
    try:
        cookie = Cookie.SimpleCookie(os.environ["HTTP_COOKIE"])
        session = cookie['usrSession'].value
        return session
    except (Cookie.CookieError, KeyError):
        return None

My cookie string its something along the lines of:

Cookie: usrSession=12345

Hope this helps

Upvotes: 1

Related Questions