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

2024/9/8 10:46:09

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?

Answer

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

import datetimeself.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') 
https://en.xdnf.cn/q/72682.html

Related Q&A

connection refused with Celery

I have a Django project on an Ubuntu EC2 node, which I have been using to set up an asynchronous using Celery. I am following How to list the queued items in celery? along with the docs, to experiment…

Routing all packets through my program?

I want to build an application that routes all network traffic (not just HTTP) through my application. Basically, what I want is all the traffic to be given to my application (they should never reach t…

python struct.pack(): pack multiple datas in a list or a tuple

Say i have a list or a tuple containing numbers of type long long,x = [12974658, 638364, 53637, 63738363]If want to struct.pack them individually, i have to use struct.pack(<Q, 12974658)or if i want…

Can I pass a list of colors for points to matplotlibs Axes.plot()?

Ive got a lot of points to plot and am noticing that plotting them individually in matplotlib takes much longer (more than 100 times longer, according to cProfile) than plotting them all at once. Howev…

Tidy data from multilevel Excel file via pandas

I want to produce tidy data from an Excel file which looks like this, with three levels of "merged" headers:Pandas reads the file just fine, with multilevel headers:# df = pandas.read_excel(t…

ttk tkinter multiple frames/windows

The following application I have created is used to demonstrate multiple windows in tkinter. The main problem is that none of the Entry controls, neither in the bmi-calculator or the converter, accept …

input() vs sys.stdin.read()

import sys s1 = input() s2 = sys.stdin.read(1)#type "s" for examples1 == "s" #False s2 == "s" #TrueWhy? How can I make input() to work properly? I tried to encode/decode…

Renormalize weight matrix using TensorFlow

Id like to add a max norm constraint to several of the weight matrices in my TensorFlow graph, ala Torchs renorm method.If the L2 norm of any neurons weight matrix exceeds max_norm, Id like to scale it…

Numpy: find the euclidean distance between two 3-D arrays

Given, two 3-D arrays of dimensions (2,2,2):A = [[[ 0, 0],[92, 92]],[[ 0, 92],[ 0, 92]]]B = [[[ 0, 0],[92, 0]],[[ 0, 92],[92, 92]]]How do you find the Euclidean distance for each vector in A and B e…

Is it possible to break from lambda when the expected result is found

I am Python newbie, and just become very interested in Lambda expression. The problem I have is to find one and only one target element from a list of elements with lambda filter. In theory, when the t…