python中会话的简单实现? [英] simple implementation of sessions in python?

查看:57
本文介绍了python中会话的简单实现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想构建一个 Web 应用程序,并了解这个过程中的技术.我为我的框架选择了 python Bottle.为了在网站上实现用户,我需要管理会话.在 python 中实现会话是否有简单"或简单"的方法?我不在乎这是否 100% 正确.我只是想了解发生了什么.

I want to build a web application and also understand the technology in the process. I chose python Bottle for my framework. To implement users on the website, I need to manage sessions. Is there an "easy" or "simple" way to implement sessions in python? I don't care about doing this 100% right. I just want to understand what's going on.

Bottle 文档 建议我使用另一个库,但我不喜欢那样,因为它增加了更多的黑匣子(阅读源代码是最终目标......).

The Bottle documentation suggests I use another library, but I don't like that because it adds more blackboxes (reading the source is an eventual goal...).

我在谷歌上搜索了实现会话 python".我刚刚找到了这个.

I've google searched "implementing sessions python". I just found this.

推荐答案

python 与它真的无关,只是查看实现会话数据"......这里是实现会话的一种方法......

python has really nothing to do with it just look into "implement session data" .... here is one way to implement sessions ...

from pyDES import * # or some other crypto library
my_app_secret = "hello crypto"


def save_session(data):
    response.set_cookie("session", triple_des(my_app_secret).encrypt(json.dumps(data), padmode=2))

def load_session():
     try:
        return json.loads(triple_des(my_app_secret).decrypt(response.get_cookie("session"))
      except:
         return {}


 session_data = load_session()
 print session_data
 session_data["some_info"] = "Yellow Submarine"
 save_session(session_data)

或者,您可以保存到数据库中,然后将哈希标识符保存在 cookie 中……或其他各种方法……(您甚至不需要对其进行加密……您可以像添加校验和字节到数据字符串的末尾或其他内容)

alternatively you could save to a database and just save a hash identifier in the cookie ... or various other methods ... (you dont even really need to encrypt it... you could have something as simple as adding a checksum byte to the end of the datastring or something)

这篇关于python中会话的简单实现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆