By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,089 Members | 2,359 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,089 IT Pros & Developers. It's quick & easy.

How to set a cookie and then redirect using Python?

P: 2
I've read that Python does not alllow one to set a cookie and then redirect. I'm trying to save the current page location before pushing the user to the signin page.

I'm coming from ASP, which allows for this in a very simple manner:
Expand|Select|Wrap|Line Numbers
  1. if request.cookies("GDtoken") = '' then
  2.   response.cookies("cpage") = request.servervariables("PATH_INFO")
  3.   response.redirect "signin.asp"
  4. end if
  5.  
... I'm surprised that I couldn't find a workaround or a solution for Python.

Here's my Python code:
Expand|Select|Wrap|Line Numbers
  1. if 'GDtoken' not in cookie:
  2.   #save the location of this page to the cookie
  3.   cookie['cpage'] = os.environ.get('SCRIPT_NAME', '/')
  4.   cookie['cpage']['expires'] = 60*30
  5.   cookie['cpage']['path'] = '/'
  6.  
  7.   #save the continuation page cookie
  8.   print 'Status: 302 Moved Temporarily'
  9.   print cookie.output()
  10.  
  11.   #and redirect user to the signin page
  12.   print 'Location: signin.py'
  13.   print
  14.   exit()
  15.  
The cookie is not set when I get to signin.py
How would one do this in Python?

Thanks!
Jan 22 '11 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.