当前位置:网站首页>Day3 -- flag state holding, exception handling and request hook
Day3 -- flag state holding, exception handling and request hook
2022-07-27 08:29:00 【qishaoawei】
State to keep
cookie Realization way
Generate cookie
from flask import Flask,make_response,request
from bak.testing.settings.config import DefaultConfig # The second kind
app=Flask(__name__)
# Set up SECRET_KEY Configuration information , You have to set , Otherwise, throw it out of order Two ways
# app.config['SECRET_KEY'] = '[email protected]#[email protected]#sdkjf0934FHJNJGI' # The first one is
app.config.from_object(DefaultConfig) # The second kind
# Generate cookie
@app.route('/set_cookie')
def set_cookie():
resp=make_response(' Set up cookie')
#name Wei Jian zhangsan Value #max_age=60 Set up 60 Seconds valid
resp.set_cookie('name','zhangsan',max_age=60)
return resp
The second way to create a folder is to create a folder py File settings configuration information 
Get print out saved cookie
from flask import Flask,make_response,request
from bak.testing.settings.config import DefaultConfig
app=Flask(__name__)
app.config.from_object(DefaultConfig)
# obtain cookie
@app.route('/get_cookie')
def get_cookie():
name=request.cookies.get('name')
resp=make_response(' obtain cookie')
print(name)
return resp
Delete saved cookie
from flask import Flask,make_response,request
from bak.testing.settings.config import DefaultConfig
app=Flask(__name__)
app.config.from_object(DefaultConfig)
# Delete cookie
@app.route('/del_cookie')
def del_cookie():
resp=make_response(' Delete cookie')
resp.delete_cookie('name')
return resp
session Realization way
Generate session
from flask import Flask,make_response,request,session
from bak.testing.settings.config import DefaultConfig # The second kind
app=Flask(__name__)
# Set up SECRET_KEY Configuration information , You have to set , Otherwise, throw it out of order Two ways
# app.config['SECRET_KEY'] = '[email protected]#[email protected]#sdkjf0934FHJNJGI' # The first one is
app.config.from_object(DefaultConfig) # The second kind
# Generate session
@app.route('/set_session')
def set_session():
session['password']='123456'
return 'session Set up the success '
Check to get session
from flask import Flask,make_response,request,session
from bak.testing.settings.config import DefaultConfig
app=Flask(__name__)
app.config.from_object(DefaultConfig)
# Check to get session
@app.route('/get_session')
def get_session():
pwd=session.get('password')
return pwd
Delete session
from flask import Flask,make_response,request,session
from bak.testing.settings.config import DefaultConfig
app=Flask(__name__)
app.config.from_object(DefaultConfig)
# Delete session
@app.route('/del_session')
def del_session():
resp=make_response(' Delete session')
resp.delete_cookie('session')
return resp
Login function realization preservation session
from flask import Flask,make_response,request,session
from bak.testing.settings.config import DefaultConfig
app=Flask(__name__)
app.config.from_object(DefaultConfig)
# Login implementation preservation session
@app.route('/login')
def login():
# Get user name and password
username=request.args.get('username')
password=request.args.get('password')
# Verify user name and password
real_username='zhangsan'
real_password='123456'
if username!=real_username:
return ' User name error '
if password!=real_password:
return ' Wrong password '
session['name']=username
return ' Login successful '
# View login account name
@app.route('/home')
def home():
username=session.get('name')
return username
# Log out
@app.route('/logout')
def logout():
resp=make_response(' Quit successfully ')
resp.delete_cookie('session')
return resp
Login successful 
exception handling
# abort() Examples of throwing exceptions with custom exception information
from flask import Flask
# Import abort
from flask import abort
@app.route('/error')
def error():
# have access to try Conditions
# Actively throw an exception Custom status code
abort(500)
@app.errorhandler(500)
def get_error(e):
return ' Something went wrong '
request hook
@app.route('/page')
def page():
print(' The page executes ')
return ' The page executes '
@app.before_first_request
def first():
print(' Execute before page request , Only once ')
@app.before_request
def before():
print(' Before each page processing request , All implemented ')
@app.after_request
# Be careful res Must return Otherwise, an error message cannot be generated cookie
def after(res):
print(' Execute after the page processing request , Error reporting may not be implemented ')
return res
@app.teardown_request
def tr(e):
print(e)
print(' Execute after the page processing request , Error reporting will also be implemented ')
边栏推荐
猜你喜欢

Teach you to build a nail warning robot hand in hand

Oppo self-developed large-scale knowledge map and its application in digital intelligence engineering

Redis configuration file download

Eval and assert execute one sentence Trojan horse

Alibaba cloud international receipt message introduction and configuration process
![[ciscn2019 southeast China division]web11 1](/img/94/61ad4f6cbbd46ff66f361462983d7a.png)
[ciscn2019 southeast China division]web11 1
![[netding cup 2020 rosefinch group]nmap 1 two solutions](/img/fa/b1349cb42b5768b7510217239ba73a.png)
[netding cup 2020 rosefinch group]nmap 1 two solutions

ERP production operation control Huaxia

MCDF top level verification scheme

Use of string type "PHP Basics"
随机推荐
How to view instances of software objects in QSIM?
Flask one to many database creation, basic addition, deletion, modification and query
My senior
Binglog backup data
JS basic knowledge - daily learning summary ①
"PHP Basics" PHP statements and statement blocks
Implementation of adding function of background user management display
regular expression
What is the real HTAP? (1) Background article
idea远程调试
Eval and assert execute one sentence Trojan horse
Redis configuration file download
Weekly learning summary
Risk control and application of informatization project
Using ecological power, opengauss breaks through the performance bottleneck
海量数据肖枫:共建共治openGauss根社区,共享欣欣向荣新生态
ERP生产作业控制 华夏
Shenzhi Kalan Temple
Idea remote debugging
Notes in "PHP Basics" PHP