Flask拾遗总汇1
栏目:随笔 发布时间:2019-12-08 07:55

除恳求目标之外,还有一个 凯发k8国际官网session 目标。它答应你在不同恳求间存储特定用户的信息。它是在 cookies 的基础上完成的,而且对 cookies 进行密钥签名要运用会话,你需求设置一个密钥。

from flask import flask,url_for,session
app = flask
app.secret_key = sdsfdgdgdgd 
app.config[ session_cookie_name ] = session_lvning #设置session的姓名
@app.route
def index:
 #session本质上操作的是字典, session是否还有其他办法?与字典办法相同
 #session的原理:假如下一次拜访的时分带着随机字符串,会把session里边对应的
 # 值拿到内存,假定session保存在数据库,每履行一次链接一次数据库,每次都要不时更新的话
 # 会十分损耗内存
 session[ xxx ] = 123
 session[ xxx2 ] = 123
 session[ xxx3 ] = 123
 session[ xxx4 ] = 123
 del session[ xxx2 ] #在这删去了,真实存储的时分是没有xxx2的
 return ddsf 
if __name__ == __main__ :
 app.run

关于session的装备

app.config[ session_cookie_name ] = session_lvning 
- session超不时刻怎么设置? permanent_session_lifetime : timedelta
 以下是跟session相关的装备文件
 session_cookie_name : session ,
 session_cookie_domain : none,
 session_cookie_path : none,
 session_cookie_httponly : true,
 session_cookie_secure : false,
 session_refresh_each_request : true, #是否每次都跟新
 permanent_session_lifetime : timedelta
 1 from flask import flask, session, redirect, url_for, escape, request
 3 app = flask
 5 @app.route
 6 def index:
 7 if username in session:
 8 return logged in as %s % escape
 9 return you are not logged in 
11 @app.route
12 def login:
13 if request.method == post :
14 session[ username ] = request.form[ username ]
15 return redirect)
16 return 
17 form action= method= post 
18 p input type=text name=username 
19 p input type=submit value=login 
20 /form 
23 @app.route
24 def logout:
25 # remove the username from the session if it s there
26 session.pop
27 return redirect)
29 # set the secret key. keep this really secret:
30 app.secret_key = a0zr98j/3yx r~xhh!jmn]lwx/,?rt 

自定义session:

 1 pip3 install flask-session
 3 run.py
 4 from flask import flask
 5 from flask import session
 6 from pro_flask.utils.session import mysessioninterface
 7 app = flask
 9 app.secret_key = a0zr98j/3yx r~xhh!jmn]lwx/,?rt 
 10 app.session_interface = mysessioninterface
 12 @app.route
 13 def login:
 14 print
 15 session[ user1 ] = alex 
 16 session[ user2 ] = alex 
 17 del session[ user2 ]
 19 return 内容 
 21 if __name__ == __main__ :
 22 app.run
 24 session.py
 25 #!/usr/bin/env python
 26 # -*- coding:utf-8 -*-
 27 import uuid
 28 import json
 29 from flask.sessions import sessioninterface
 30 from flask.sessions import sessionmixin
 31 from itsdangerous import signer, badsignature, want_bytes
 34 class mysession:
 35 def __init__:
 36 self.sid = sid
 37 self.initial = initial
 38 super.__init__)
 41 def __setitem__:
 42 super.__setitem__
 44 def __getitem__:
 45 return super.__getitem__
 47 def __delitem__:
 48 super.__delitem__
 52 class mysessioninterface:
 53 session_class = mysession
 54 container = {}
 56 def __init__:
 57 import redis
 58 self.redis = redis.redis
 60 def _generate_sid:
 61 return str)
 63 def _get_signer:
 64 if not app.secret_key:
 65 return none
 66 return signer
 69 def open_session:
 71 程序刚启动时履行,需求回来一个session目标
 73 sid = request.cookies.get
 74 if not sid:
 75 sid = self._generate_sid
 76 return self.session_class
 78 signer = self._get_signer
 79 try:
 80 sid_as_bytes = signer.unsign
 81 sid = sid_as_bytes.decode
 82 except badsignature:
 83 sid = self._generate_sid
 84 return self.session_class
 86 # session保存在redis中
 87 # val = self.redis.get
 88 # session保存在内存中
 89 val = self.container.get
 91 if val is not none:
 92 try:
 93 data = json.loads
 94 return self.session_class
 95 except:
 96 return self.session_class
 97 return self.session_class
 99 def save_session:
101 程序完毕前履行,能够保存session中所有的值
102 如:
103 保存到resit
104 写入到用户cookie
106 domain = self.get_cookie_domain
107 path = self.get_cookie_path
108 httponly = self.get_cookie_httponly
109 secure = self.get_cookie_secure
110 expires = self.get_expiration_time
112 val = json.dumps)
114 # session保存在redis中
115 # self.redis.setex
116 # session保存在内存中
117 self.container.setdefault
119 session_id = self._get_signer.sign)
121 response.set_cookie

第三方flask-session运用:

session存在在服务端的一个字典里边,session保存起来,取一次里边仍是有的,直到你删去之后才没有了

from flask import flask, session, render_template, request, views
from flask_session import session
from redis import redis
app = flask
app.default_config
app.config[ session_type ] = redis 
app.config[ session_redis ] = redis
app.templates_auto_reload = true
# 运用第三方flask-session,封装app这个实例化目标
session
userinfo = { user : alex , pwd : 123 }
下一篇:没有了
服务热线