cookie and session usage and differences

And set the cookie expiration time

@app.route('/cookie')
def set_cookie():
    response = make_response('hello world')
    response.set_cookie('username', 'itheima', max_age=3600)
    return response

Get cookie

from flask import Flask,request
#获取cookie
@app.route('/request')
def resp_cookie():
    resp = request.cookies.get('username')
    return resp

 Get Set the session data

session: the request context object for processing some of the data content http request

@app.route('/set_session')
def set_session():
    session['username'] = 'itcast'
    return 'set_session ok!'

@app.route('/get_session')
def get_session():
    return session.get('username')

 The difference between the cookie and session

1. cookie stored on the client side, a small amount of data, 4k, the default browser is closed, can set their own
2. session on the server, depending on the cookie, session_id, unlimited storage, the default is valid 14 days

Guess you like

Origin blog.csdn.net/qwertyuiopasdfgg/article/details/93332534