HttpResponse 2. Cookie

A profile,

The view must return an HttpResponse object after receiving an HttpRequest and processing it. For now, the view function just returns a string, which Flask then converts into a response object. If you want to explicitly convert, you can use the make_response() function and then modify it.

Second, the HttpResponse

Let’s create a new template named cookie. HTML and simply write the following code:

<! DOCTYPE HTML > < HTML lang="en"> <head> <meta charset="UTF-8"> <title> title </title> </head> <body> <h1> Set cookie</h1> </body> </html>Copy the code

We return HttpResponse behind the scenes with make_response() :

from flask import Flask, render_template, make_response

app = Flask(__name__)


@app.route('/setcookie', methods=['GET', 'POST']
def setcookie(a):  View function
    resp = make_response(render_template('cookie.html'))  Explicitly convert to an HttpResponse object
   
    return resp


app.config['DEBUG'] = True

if __name__ == '__main__':
    # 0.0.0.0 means that any address that represents the machine is accessible
    app.run(host='0.0.0.0', port=5000)  # run the program

Copy the code

Let’s go to the browser and try it:

Three, Cookie,

We all know that HTTP protocol is stateless request protocol, the user this visit and the next visit is a new request, there is no relationship between them. But we need to know what the last user did, so we need to use cookies. A cookie is a piece of plain text information stored in a browser by a website in key-value pair format for user tracking. Cookies are domain security-based and we set cookies with the set_cookie method

from flask import Flask, render_template, make_response

app = Flask(__name__)


@app.route('/setcookie', methods=['GET', 'POST']
def setcookie(a):  View function
    resp = make_response(render_template('cookie.html'))  Explicitly convert to an HttpResponse object
    resp.set_cookie('nickname'.'laowang')  # set the cookie
    resp.set_cookie('age'."12")  # set the cookie
    return resp


app.config['DEBUG'] = True

if __name__ == '__main__':
    # 0.0.0.0 means that any address that represents the machine is accessible
    app.run(host='0.0.0.0', port=5000)  # run the program

Copy the code

Let’s go to the browser and try it:

HttpReqeust: HttpReqeust: HttpReqeust: HttpReqeust: HttpReqeust

@app.route('/getcookie', methods=['GET', 'POST']
def getcookie(a):  View function
    nickname = request.cookies.get('nickname')
    age = request.cookies.get('age')
    return 'nickname=%s age=%s' % (nickname, age)
Copy the code

Let’s go to the browser and try it:

Expiration Date:

Cookies have an expiration time. When we set cookies, we can directly set the expiration time for cookies. There are several Settings as follows:

  • Max_age is an integer that expires after a specified number of seconds.
  • An expires is a datetime or timedelta object at which the session will expire.
  • Choose max_age or Expires.
  • If the expiration time is not specified, the cookie will expire when the browser is closed.
from flask import Flask, request, render_template, make_response
from datetime import datetime

app = Flask(__name__)


@app.route('/setcookie', methods=['GET', 'POST']
def setcookie(a):  View function
    resp = make_response(render_template('cookie.html'))  Explicitly convert to an HttpResponse object
    resp.set_cookie('nickname'.'laowang', max_age=3600)  Set cookies to expire in 3600 seconds
    resp.set_cookie('age'."12", expires=datetime(2019.3.18))  # Set cookie, expire after March 18,2019
    return resp


@app.route('/getcookie', methods=['GET', 'POST']
def getcookie(a):  View function
    nickname = request.cookies.get('nickname')
    age = request.cookies.get('age')
    return 'nickname=%s age=%s' % (nickname, age)


app.config['DEBUG'] = True

if __name__ == '__main__':
    # 0.0.0.0 means that any address that represents the machine is accessible
    app.run(host='0.0.0.0', port=5000)  # run the program

Copy the code

Let’s go to the browser and try it:

Delete the cookies:

We can delete cookies by using the delete_cookie method

@app.route('/delcookie', methods=['GET', 'POST']
def delcookie(a):  View function
    res = make_response('delete cookies')
    res.delete_cookie('nickname')  The essence of deleted cookies is to change the expiration time of cookies
    return res
Copy the code

Welcome to follow my official account: