2 Star 1 Fork 3

keijack / python-simple-http-server-redis-session

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

Python Simple Http Server 的 Redis Session 实现

这是什么?

这是工程 https://gitee.com/keijack/python-simple-http-server 的分布式 session 的 redis 实现。

如何使用?

安装本工程之后,参考以下代码配置

import simple_http_server
import simple_http_server.server as server
import os
from simple_http_server_redis_session.http_session_redis_impl import RedisSessionFactory

def main(*args):
    # host, port, db, username, password 均为可选。
    simple_http_server.set_session_factory(RedisSessionFactory(host="10.0.2.16", port=6379, db=0, username="", password=""))
    server.scan("tests/ctrls", r'.*controllers.*')
    
    root = os.path.dirname(os.path.abspath(__file__))
    server.start(
        port=9090,
        resources={"/public/*": f"{root}/tests/static"})

当然,你也可以重用你已经定义好的在别的逻辑代码中使用的 redis-client:

import simple_http_server
import simple_http_server.server as server
import os
import redis
from simple_http_server_redis_session.http_session_redis_impl import RedisSessionFactory

def main(*args):
    # This redis client can be used in other business codes.
    redis_client = redis.Redis(host="10.0.2.16", port=6379, db=0, username="", password="")
    simple_http_server.set_session_factory(RedisSessionFactory(redis_client=redis_client))
    
    root = os.path.dirname(os.path.abspath(__file__))
    server.start(
        port=9090,
        resources={"/public/*": f"{root}/tests/static"})

编写你自己的序列化逻辑

默认情况下,我们会使用 pickle 来序列化和反序列化类,如果上述的序列化逻辑无法满足你的需求,你可以编写自己的序列化逻辑。

simple_http_server_redis_session.http_session_redis_impl import ObjectSerializer

class MyObjectSerializer(ObjectSerializer):

    def object_to_bytes(self, obj: Any) -> bytes:
        bys = ...
        return bys

    def bytes_to_objects(self, value: bytes) -> Any:
        obj = ...
        return obj

# 在初始化 SessionFactory 的时候加上这个对象参数。
simple_http_server.set_session_factory(RedisSessionFactory(host="10.0.2.16", port=6379, db=0, username="", password="", object_serializer=MyObjectSerializer()))
MIT License Copyright (c) 2018 Keijack Wu Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

python-simple-http-server 的分布式 session 的 redis 实现。 展开 收起
Python
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Python
1
https://gitee.com/keijack/python-simple-http-server-redis-session.git
git@gitee.com:keijack/python-simple-http-server-redis-session.git
keijack
python-simple-http-server-redis-session
python-simple-http-server-redis-session
main

搜索帮助