flash-session

本文详细介绍了如何在Flask应用中更改session的存储位置,从默认的浏览器cookie到使用如Redis、Memcached等外部存储服务。通过示例代码展示了配置和使用不同session接口的方法。
部署运行你感兴趣的模型镜像

作用:更改session存储的位置

1、session默认存放在浏览器的cookie中

源码

wsgi->app.__call__->wsgi_app->push->self.app.session_interface->session_interface = SecureCookieSessionInterface()->open_session和save_session

2、更改session储存的位置

a、下载

pip3 install -i https://pypi.douban.com/simple flask-session

b、源码分析

Session->self.init_app(app)->app.session_interface = self._get_interface(app)->session_interface

other

MemcachedSessionInterface
FileSystemSessionInterface
MongoDBSessionInterface
SqlAlchemySessionInterface

具体看源码

c、示例

import redis
from flask import Flask, session
# 1.导入模块
from flask_session import Session
# from flask.sessions import SecureCookieSession  # 原始
# from flask_session import RedisSessionInterface # 修改
app = Flask(__name__)

# app.session_interface = SecureCookieSessionInterface()
# app.session_interface = RedisSessionInterface()
# 2.修改配置文件
app.config['SESSION_TYPE'] = 'redis'
app.config['SESSION_REDIS'] = redis.Redis(host='ip', port=6379, password='密码')
# 3.注册
Session(app)


@app.route('/login')
def login():
    session['user'] = 'alex'
    return 'Hello'


@app.route('/home')
def index():
    print(session.get('user'))

    return 'World'


if __name__ == '__main__':
    app.run()

 

转载于:https://www.cnblogs.com/wt7018/p/11605411.html

您可能感兴趣的与本文相关的镜像

Stable-Diffusion-3.5

Stable-Diffusion-3.5

图片生成
Stable-Diffusion

Stable Diffusion 3.5 (SD 3.5) 是由 Stability AI 推出的新一代文本到图像生成模型,相比 3.0 版本,它提升了图像质量、运行速度和硬件效率

并非只有Flask框架用到了sessionsession机制是Web开发中用于跟踪用户会话状态的一种常用技术,许多其他Web框架也广泛使用了session。 例如,在Python的Django框架中,也有完善的session机制。Django的session默认存储在数据库中,也可以配置为存储在缓存(如Redis)中。以下是一个简单的Django使用session的示例: ```python # 在视图函数中使用session from django.http import HttpResponse def set_session(request): request.session['key'] = 'value' return HttpResponse('Session set') def get_session(request): value = request.session.get('key') return HttpResponse(f'Session value: {value}') ``` 在Java的Spring框架里,同样支持session管理。Spring可以通过`HttpSession`对象来存取session数据,示例代码如下: ```java import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class SessionController { @GetMapping("/setSession") public String setSession(HttpServletRequest request) { HttpSession session = request.getSession(); session.setAttribute("key", "value"); return "Session set"; } @GetMapping("/getSession") public String getSession(HttpServletRequest request) { HttpSession session = request.getSession(); String value = (String) session.getAttribute("key"); return "Session value: " + value; } } ``` 在Node.js的Express框架中,也可以使用`express-session`中间件来实现session功能,示例如下: ```javascript const express = require('express'); const session = require('express-session'); const app = express(); app.use(session({ secret: 'your-secret-key', resave: false, saveUninitialized: true })); app.get('/setSession', (req, res) => { req.session.key = 'value'; res.send('Session set'); }); app.get('/getSession', (req, res) => { const value = req.session.key; res.send(`Session value: ${value}`); }); app.listen(3000, () => { console.log('Server is running on port 3000'); }); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值