Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set cookies with GAE/Python for 1 month?

I need to implement the following:

  1. User input user id and pass
  2. We validate that on another server
  3. If they are correct, cookies with these details should be saved for one month
  4. Each time user uses my site, we should look for cookies
  5. If they are not found - go to step 1

How can I set cookies for 1 month?

Will the following work?

self.response.headers.add_header(
        'Set-Cookie', 
        'credentials=%s; expires=Fri, 31-Dec-2020 23:59:59 GMT' \
          % credentials.encode())

How to calculate one month from now in the required format?

like image 914
LA_ Avatar asked Sep 25 '11 07:09

LA_


1 Answers

You can use webapp.Response.set_cookie() method:

import datetime

self.response.set_cookie('name', 'value', expires=datetime.datetime.now(), path='/', domain='example.com')

Formatting dates for cookies is something like this:

print (datetime.datetime.now() + datetime.timedelta(weeks=4)).strftime('%a, %d %b %Y %H:%M:%S GMT') 
like image 169
mre666 Avatar answered Nov 19 '22 21:11

mre666