Handles all the passing the session data to the browser
Allows us to retrieve session data
Handles all the receiving the session data from the browser
Detour: Encryption
Example of simple encryption
Secret Keys
What is a Secret Key?
Used to encrypt the content of the cookie so that they can't be read (without the encryption key and the algorithm).
If they are changed, the server will reject the cookie and not use it.
Secret Keys
Creating a Secret Key
Run this code once, outside of your program:
import secrets
secrets.token_hex(32)
It will produce a string which looks something like this: '8f42a73054b1749f8f58848be5e6502c'
Secret Keys
Putting a Secret Key into your Application
Copy and paste that string into your program as the value of app.config['SECRET_KEY']
Flask - Session
from flask importFlask, request, sessionapp=Flask(__name__)
app.config['SECRET_KEY'] ='8f42a73054b1749f8f58848be5e6502c'
works as a dictionary for storing, checking if it has, and retrieving
some useful extras:
session.clear()
session.modified = True (when we change an int, float, string or boolean, it automatically notes that the session has been modified and needs to be stored again, on the other hand, when we append to an existing list, the session doesn't notice, and we need to manually set the modified state to True)
Also Useful
session.get('variable', default_value)
Demo 1
Create a simple application which saves a range of things in cookies.
As the user, how do we get rid of the cookie?
Demo 2
Write the Guess My Number application so that it stores the guess, range_high and range_low in a session object (and saved as cookies).
Demo 3
Shopping list (storing lists and dictionaries in the session).