Trying to set a cookie on localhost to test

22 views
Skip to first unread message

David Montgomery

unread,
Feb 11, 2012, 2:12:03 PM2/11/12
to web. py
Hi,

I am trying to set a cookie using the below. Should it not work on
localhost? I also tried localhost.localdomain.

So..did I set a cookie correct? If I go back to the page should I
then should to read the cookie?

I am a cookie newbie.

class index:
def GET(self):
env = web.ctx['environ']
qs = urlparse.parse_qs(env['QUERY_STRING'])

#Get cookie
cookie = web.cookies().get('test')
#Set cookie

web.setcookie('test', "rtb", expires=3600, domain='localhost',
secure=False)

return "<html></html>"

Thanks

Tom

unread,
Feb 12, 2012, 7:18:38 AM2/12/12
to web.py
David,

It looks like you are trying to get the cookie before you set it. I
believe the way you have it currently, the cookie will never be set,
as an exception will be thrown before it gets to the point in the
script where it will be set.

You can either move the web.setcookie() call to before
"cookie=web.cookies().get('test')", or, if you want to always try and
retrieve it first, do,

try:
cookie = web.cookies().get('test')
except AttributeError: #I think it's an AttributeError, anyway.
web.setcookie([...])

On Feb 11, 2:12 pm, David Montgomery <davidmontgom...@gmail.com>
wrote:
Reply all
Reply to author
Forward
0 new messages