1. flask扩展

说明:

flask的扩展类似于python中的装饰器,和Django中的process_request的方法也类似

测试代码

from flask import Flask,session,Session,flash,get_flashed_messages,redirect,render_template,request
app = Flask(__name__)
app.secret_key ='sdfsdfsdf' @app.before_request
def process_request1():
print('process_request1') @app.after_request
def process_response1(response):
print('process_response1')
return response @app.before_request
def process_request2():
print('process_request2') @app.after_request
def process_response2(response):
print('process_response2')
return response @app.route('/index')
def index():
print('index')
return 'Index' @app.route('/order')
def order():
print('order')
return 'order' @app.route('/test')
def test():
print('test')
return 'test' if __name__ == '__main__':
app.run()

在浏览器中访问index结果:

process_request1
process_request2
127.0.0.1 - - [04/Jan/2018 15:48:53] "GET / HTTP/1.1" 404 -
process_response2
process_response1
127.0.0.1 - - [04/Jan/2018 15:48:58] "GET /index HTTP/1.1" 200 -
process_request1
process_request2
index
process_response2
process_response1

实际使用案例:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:supery from flask import Flask,render_template,request,redirect,session app = Flask(__name__)
# 使用session需要配置secret_key
app.secret_key ='sdfsdfsdf' @app.route('/login',endpoint='login',methods=['POST','GET'])
def login():
if request.method == "GET":
return render_template('login.html')
else:
user = request.form.get('username')
pwd = request.form.get('password')
if user == 'alex' and pwd =='':
# 创建session信息
session['user_info'] = {'user':user,'userID':1}
return redirect('index')
return render_template('login.html') @app.before_request
def process_request():
if request.path == '/login':
return None
if not session.get('user_info'):
return redirect('login') @app.after_request
def process_response(response):
print('写扩展程序')
return response @app.route('/index',endpoint='index',methods=['GET','POST'])
def index():
return 'index' @app.route('/query',endpoint='query',methods=['GET','POST'])
def query():
return 'query' @app.route('/student',endpoint='student',methods=['GET','POST'])
def student():
return 'student' if __name__ == '__main__':
app.run()

扩展登录验证

Flask 通过扩展来实现登录验证的更多相关文章

  1. python装饰器 & flask 通过装饰器 实现 单点登录验证

    首先介绍装饰器,以下是一段标注了特殊输出的代码.用于帮助理解装饰器的调用过程. import time def Decorator_one(arg1): info = "\033[1;31; ...

  2. Flask【第4篇】:用Flask的扩展实现的简单的页面登录

    用flask的扩展实现的简单的页面登录 from flask import Flask,render_template,request,redirect,session app = Flask(__n ...

  3. Flask常用扩展(Extentions)

    Flask常用扩展(Extentions) 官网;http://flask.pocoo.org/extensions/ 1.Flask-Script ​ 说明: 一个flask终端运行的解析器 安装: ...

  4. nodeJS---express4+passport实现用户注册登录验证

    网上有很多关于passport模块使用方法的介绍,不过基本上都是基于express3的,本文介绍在express4框架中使用passport模块. 前言 passport是一个功能单一,但非常强大的一 ...

  5. php 连接redis,并登录验证

    环境: centos7 上安装了redis, 同时安装了php的redis扩展 yum install redis yum install php-pecl-redis redis服务端设置了登录密码 ...

  6. 搭建开发框架Express,实现Web网站登录验证

    NodeJS学习笔记(一)——搭建开发框架Express,实现Web网站登录验证   JS是脚本语言,脚本语言都需要一个解析器才能运行.对于写在HTML页面里的JS,浏览器充当了解析器的角色.而对于需 ...

  7. PHP连接LDAP进行登录验证

    基于安全性考虑,准备把PHP做的自动化平台加入ldap登录验证,具体做法如下: 了解背景: LDAP 的全称是"轻量级目录访问协议(Lightweight Directory Access ...

  8. djangorestframework-jwt自带的认证视图进行用户登录验证源代码学习

    Django REST framework JWT djangorestframework-jwt自带的认证视图进行用户登录验证源代码学习 SECRET_KEY = '1)q(f8jrz^edwtr2 ...

  9. DJango简单的后台定义登录验证

    第一步创建一个新的项目 APPLICATIONNAME : 表示创建子项目 第二步:找到主项目的url 进行 include 分发式url 简单的说,就是将app里边的url放在这里. 这里也可以找到 ...

随机推荐

  1. mybatis主键返回的实现

    向数据库中插入数据时,大多数情况都会使用自增列或者UUID做为主键.主键的值都是插入之前无法知道的,但很多情况下我们在插入数据后需要使用刚刚插入数据的主键,比如向两张关联表A.B中插入数据(A的主键是 ...

  2. [实战篇入门]01-POI读Excel

    这一章的内容就是告诉各位同学如何入门POI的简单使用,再之后我们还会学习如何封装模版,由于个人时间问题,不定期更新!如果有需要,请再QQ中联系我,好了,开始工作! 新建一个Java项目,首先需要一些列 ...

  3. 数学:拓展BSGS

    当C不是素数的时候,之前介绍的BSGS就行不通了,需要用到拓展BSGS算法 方法转自https://blog.csdn.net/zzkksunboy/article/details/73162229 ...

  4. Data Mining的十种分析方法——摘自《市场研究网络版》谢邦昌教授

    Data Mining的十种分析方法: 记忆基础推理法(Memory-Based Reasoning:MBR)        记忆基础推理法最主要的概念是用已知的案例(case)来预测未来案例的一些属 ...

  5. Ubuntu12.04 GIT安装和使用

    一.安装GIT和配置GIT 1.安装GIT apt-get install git 2.配置GIT ##配置用户信息 git config --global user.name "John ...

  6. .net core 安装Swagger

    Install-Package Swashbuckle -Pre 1.Startup // This method gets called by the runtime. Use this metho ...

  7. Git 常见工作流

    多种多样的工作流使得在项目中实施Git时变得难以选择.这份教程提供了一个出发点,调查企业团队最常见的Git工作流. 阅读的时候,请记住工作流应该是一种规范而不是金科玉律.我们希望向你展示所有工作流,让 ...

  8. UOJ#31 【UR #2】猪猪侠再战括号序列

    传送门http://uoj.ac/problem/31 大家好我是来自百度贴吧的_叫我猪猪侠,英文名叫_CallMeGGBond. 我不曾上过大学,但这不影响我对离散数学.复杂性分析等领域的兴趣:尤其 ...

  9. 【Luogu】 P3928 SAC E#1 - 一道简单题 Sequence2

    [题目]洛谷10月月赛R1 提高组 [算法]递推DP+树状数组 [题解]列出DP递推方程,然后用树状数组维护前后缀和. #include<cstdio> #include<cstri ...

  10. Spring理论基础-控制反转和依赖注入

    第一次了解到控制反转(Inversion of Control)这个概念,是在学习Spring框架的时候.IOC和AOP作为Spring的两大特征,自然是要去好好学学的.而依赖注入(Dependenc ...