Response
Response Object:
When a Web request is processed by the server, the response of the user request is returned, and the response object is used, depending on the response to the user, the response object has the following processing methods
1. If a legitimate response object is returned, it is returned directly from the view.
2. If a string is returned, the response object is created with the string data and default parameters.
3. If a tuple is returned, the elements in the tuple can provide additional information. Such tuples must be in the form of (response, status, headers) and contain at least one element. The status value overrides the state code, and headers can be a list or dictionary as an additional message header value.
4. If none of the above conditions are met, Flask assumes that the return value is a legitimate WSGI application and is converted to a Request object.
The above quote from the official website, in fact, simply means that all responses will generate a legitimate response object, we usually use the most is to return the string directly or return a template. Will eventually be processed into response objects by flask.
Cookies object:
Cookies are used to store some information on the client, and when the flask application responds to user requests, the value of the cookie can be set, and the value of the cookie can be obtained when the user requests the Flask application.
To put it simply, cookies are set up in the following ways:
Responseobj.set_cookies ("xxx"), here the Responseobj is a response instance, XXX is the name of the cookie
The way to obtain cookies is
Request.cookies.get ("xxx"), where request is the global Request object, XXX is the name of the cookie
Here is an example of how cookies are used
Create a way to set up cookies:
@app. Route () setcookies (): res = Make_response () Res.set_cookie (,) Res
Note: The import Make_response method is required
2. Create a method of obtaining cookies
@app. Route () GetCookies (): Mycookies = Request.cookies.get () mycookies
3. Test your application
Accessing the/setcookies method
Accessing the/getcookies method
Session object:
It allows you to store information about a particular user between requests. It is implemented on the basis of cookies and the key signature of the cookies. This means that the user can view the contents of your Cookie, but cannot modify it unless the user knows the signed key.
Session can be set by session[' xxx ']= ' yyy ', where xxx is the key value, YYY is the session value.
by Session.pop (' xxx ', none) the key is XXX's session removed
Verification of whether the user is logged in using the session normally
For example, the following:
Set session
Use the following code
@app. Route () setsession (): session[] = session[]
Use the session to set the App_secret before the error:
Runtimeerror:the session is unavailable because no secret key was set. Set the Secret_key on the application to something unique and secret.
Add the Secret_key setting before calling this code
App.secret_key = "1234"
For security reasons, the Secret_key is a bit more complicated to set up, and the demo is easy to set up here.
2. Verify session
The code is as follows:
@app. Route () Checksession (): Session::
3. Remove session
The code is as follows:
@app. Route () removesession (): Session.pop (,)
4. Verification
Visit/setsession
Verify Session Access/checksession
Prove that the session exists
Next, remove session
Then we verify the session again.
Session is missing
The use of response, cookies and session objects in Python web development-flask