Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 :)

like image 844
Kryštof Hilar Avatar asked Aug 04 '26 10:08

Kryštof Hilar


1 Answers

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

like image 69
Riley Avatar answered Aug 06 '26 23:08

Riley



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!