Re: Cookie Confusion - How to Set a Cookie



On Apr 28, 9:42 am, cbh...@xxxxxxxxx wrote:
....I see the cookie in my HTTP header
but do not get anything in the cookie text file. I'm working on
linux.

print "Content-type: text/html"
cookie = Cookie.SimpleCookie()
cookie['Test'] = 'abc'
print cookie
print

Are there rules about where in the header the set cookie line should
be?

Hi Christian. I think the cookie can go anywhere in
the header, but I usually put it before the content-type.
If you want to store the cookie to a file,
or even better, to a database of some sort, you have to
do it yourself, the Cookie module doesn't do it for you,
I hope.

# store cookie to /tmp/cookie.txt
file("/tmp/cookie.txt","w").write(str(cookie))

For parsing cookies, I stole and modified this from
the Django source (for use in a cgi script):

===
from Cookie import SimpleCookie
import os

# stolen and modified from Django
def parse_cookie(cookie=None, environ=None):
if cookie is None:
if environ is None:
environ = os.environ
cookie = environ.get('HTTP_COOKIE', '')
if cookie == '':
return {}
c = SimpleCookie()
c.load(cookie)
cookiedict = {}
for key in c.keys():
cookiedict[key] = c.get(key).value
return cookiedict

===
All the best. -- Aaron Watters

===
http://www.xfeedme.com/nucular/pydistro.py/go?FREETEXT=monster
.



Relevant Pages

  • Re: Cookie Confusion - How to Set a Cookie
    ... but do not get anything in the cookie text file. ... if environ is None: ... Upon a successful login, I want to write their name ...
    (comp.lang.python)
  • Re: Cookies and CookieJar
    ... Set a Cookie, without checking with policy to see whether or not it ... The Cookie mentioned there is a an object of the Cookie class which is ... Cookie.SimpleCookie() creates an object of a class called SimpleCookie ... A "cookie" is just one of the headers in a request. ...
    (comp.lang.python)