如何在 Python Web 应用程序中启动会话? [英] How do I start a session in a Python web application?

查看:40
本文介绍了如何在 Python Web 应用程序中启动会话?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题基于 这个答案.

我正在为 Python 寻找类似于 PHP session_start() 的函数.我想在 PHP 中访问像 $_SESSION 这样的字典,它在运行命令后可用.

I'm looking for a function similar to PHP's session_start() for Python. I want to access a dictionary like $_SESSION in PHP which becomes available after running the command.

推荐答案

让我解决一些可能与您的问题相关的事情...它可能与您无关,但我认为其他人可能会带着确切的问题来到这里同样的问题,可能会从我(有限的)经验中受益...因为我曾经也遇到过这个问题.

Let me address some things that might be related to your question...it may not be relevant for you, but I think others might come here with the exact same question and might benefit from my (limited) experience...because I also had this question at one time.

作为一个从 PHP 转向 Python(并且从未回头)的人,我认为了解会话在幕后的工作原理很有用.实现自己的会话框架可能可能不是一个好主意,除非您 (a) 想通过做或 (b) 需要现有框架不提供的东西来了解有关会话管理的更多信息.

Speaking as someone who went from PHP to Python (and never looked back), I think it's useful to understand how sessions work under the hood. It's probably not a good idea to implement your own session framework unless you (a) want to understand more about sessions management by doing or (b) need something that existing frameworks don't offer.

维基百科总是一个不错的起点.底线:会话数据存储在服务器上的某个地方,并由唯一标识符(某种散列)索引.该标识符在客户端和服务器之间来回传递,通常作为 cookie 或作为查询字符串(URL)的一部分.为了安全起见,您需要使用 SSL 连接或使用其他一些数据(例如 IP 地址)验证会话 ID.默认情况下,PHP 将会话存储为文件,但在共享服务器上可能会带来安全风险,因此您可能希望覆盖会话引擎,以便将会话存储在数据库中.Python Web 框架具有类似的功能.

Wikipedia is always a good place to start. Bottom line: session data gets stored somewhere on the server and indexed by a unique identifier (hash of some sort). This identifier gets passed back and forth between the client and server, usually as a cookie or as part of the query string (the URL). For security's sake, you'll want to use an SSL connection or validate the session ID with some other piece of data (e.g. IP address). By default PHP stores sessions as files, but on a shared server that could pose a security risk, so you might want to override the session engine so you store sessions in a database. Python web frameworks have similar functionality.

当我开始用 Python 进行 Web 编程时,我注意到两件事.首先,PHP 在语言中融入了很多魔力,使初学者(2003 年的我)很容易学习这门语言,但并没有教我很多关于一切如何运作的知识.因此,我发现自己研究了许多关于 Web 应用程序的主题,特别是数据库连接池、URL 映射、会话和线程.PHP(和 Django,据我所知)为您抽象出来.其次,PHP 是一种非常糟糕的语言 ;) 但它完成了工作!!

When I started doing web programming in Python, I noticed two things. First, PHP wrapped a lot of magic into the language, making it easy for a beginning programmer (me in 2003) to learn the language, but not teaching me much about how everything worked. Therefore, I found myself researching many topics about web applications, specifically database connection pooling, URL mapping, sessions, and threading. PHP (and Django, from what I understand) abstract that away for you. Second, PHP is a really crappy language ;) but it gets the job done!!

我个人使用 CherryPy 进行 Web 开发.它具有会话管理作为您可以打开的工具".

Personally I use CherryPy for web development. It has session management as a "tool" that you can turn on.

这篇关于如何在 Python Web 应用程序中启动会话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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