IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

bottle-session非路由参数方式的调用方法

IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

一般bottle-session的使用要在路由中以 def callback(session) 的方式调用,对于已有的网站改造比较麻烦,现有一种方法进行改造,具体如下: 新建一类:mybottlesession.py

import redis
import uuid
from bottle_session import Session
from config import redis_config
from common import log_helper

class MySession():
    """A bottle session object .
    基于redis 及bottle-session.py
    以非插件的形式获取session,方便进行权限验证
    注意:cookie_name须与bottle_session中一致
    一般在globalvar.py中进行应用,集中在web_helper.py中get_session()调用
    """

    def __init__(self,host='localhost',port=6379,db=0,cookie_name='bottle.session',cookie_lifetime=600,keyword='session',password=None):
        self.host = host
        self.port = port
        self.db = db
        self.cookie_name = cookie_name
        self.cookie_lifetime = cookie_lifetime
        self.keyword = keyword
        self.password = password
        self.connection_pool = None
        self.session=None

    def SetConnection_pool(self,connection_pool):
        self.connection_pool=connection_pool
    def GetSession(self):
        if self.session is not None:
            print('无需重新生成,直接返回session')
            return self.session
        if self.connection_pool is None:
            self.connection_pool = redis.ConnectionPool(host=self.host, port=self.port, db=self.db, password=self.password)
        r = redis.Redis(connection_pool=self.connection_pool)
        self.session=Session(r,self.cookie_name,self.cookie_lifetime)
        return self.session


新建一文件globalvar.py进行引用:

import bottle
import bottle_session
from config import redis_config
import redis
from common import mybottlesession,log_helper

def _init():

    global app
    global plugin
    global m_session
    app = bottle.default_app()#bottle.Bottle()#
    #app = bottle.app()
    # 设置redis配置参数

    _redis = redis_config.REDIS_SESSION
    pool = redis.ConnectionPool(max_connections=5,host=_redis.get('server','localhost'),port=_redis.get('port',6379),db=_redis.get('db',0),password=_redis.get('pwd', ''))
    plugin = bottle_session.SessionPlugin()
    plugin.connection_pool=pool
    plugin.cookie_lifetime=600

    #plugin = bottle_session.SessionPlugin(cookie_lifetime=600)
    #plugin = bottle_session.SessionPlugin(cookie_lifetime=600,host=_redis.get('server','localhost'),port=_redis.get('port',6379),db=_redis.get('db',0),password=_redis.get('pwd', None))
    app.install(plugin)
    #=============================生成全局session变量
    m_session =mybottlesession.MySession()
    try:

        m_session.SetConnection_pool(pool)
    except Exception as e:
        log_helper.info('连接session-redis出错:(' + str(_redis) + ')' + str(e.args))
        pass

def getApp():
    return app

def getSession():
    return m_session.GetSession()

主程序中调用:

#=========使用bottle-session,redis方式实现,可跨域使用。
import globalvar as gl
# 函数主入口
if __name__ == '__main__':
    #加入session插件
    gl._init()
    run(app=gl.getApp(),host='0.0.0.0', port=8090, server=GeventWebSocketServer, debug=True)

改造完成后替换原来beaker,改动比较少,同时支持跨域使用session

文章永久链接:https://tech.souyunku.com/?p=42181


Warning: A non-numeric value encountered in /data/wangzhan/tech.souyunku.com.wp/wp-content/themes/dux/functions-theme.php on line 1154
赞(78) 打赏



未经允许不得转载:搜云库技术团队 » bottle-session非路由参数方式的调用方法

IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码
IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

评论 抢沙发

大前端WP主题 更专业 更方便

联系我们联系我们

觉得文章有用就打赏一下文章作者

微信扫一扫打赏

微信扫一扫打赏


Fatal error: Uncaught Exception: Cache directory not writable. Comet Cache needs this directory please: `/data/wangzhan/tech.souyunku.com.wp/wp-content/cache/comet-cache/cache/https/tech-souyunku-com/index.q`. Set permissions to `755` or higher; `777` might be needed in some cases. in /data/wangzhan/tech.souyunku.com.wp/wp-content/plugins/comet-cache/src/includes/traits/Ac/ObUtils.php:367 Stack trace: #0 [internal function]: WebSharks\CometCache\Classes\AdvancedCache->outputBufferCallbackHandler() #1 /data/wangzhan/tech.souyunku.com.wp/wp-includes/functions.php(5109): ob_end_flush() #2 /data/wangzhan/tech.souyunku.com.wp/wp-includes/class-wp-hook.php(303): wp_ob_end_flush_all() #3 /data/wangzhan/tech.souyunku.com.wp/wp-includes/class-wp-hook.php(327): WP_Hook->apply_filters() #4 /data/wangzhan/tech.souyunku.com.wp/wp-includes/plugin.php(470): WP_Hook->do_action() #5 /data/wangzhan/tech.souyunku.com.wp/wp-includes/load.php(1097): do_action() #6 [internal function]: shutdown_action_hook() #7 {main} thrown in /data/wangzhan/tech.souyunku.com.wp/wp-content/plugins/comet-cache/src/includes/traits/Ac/ObUtils.php on line 367