How to set a cookie with GAE / Python for 1 month?

I need to implement the following:

  • The user enters the user ID and passes
  • We check that on another server
  • If they are correct, cookies with this data must be stored for one month.
  • Every time a user uses my site, we must look for cookies
  • If they are not found, go to step 1

How to set a cookie for 1 month?

Will there be 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 a month later in the desired format?

+5
source share
1 answer

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

import datetime

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

The formatting of dates for cookies looks something like this:

print (datetime.datetime.now() + datetime.timedelta(weeks=4)).strftime('%a, %d %b %Y %H:%M:%S GMT') 
+10

All Articles