python:与cgi脚本中的会话交互

发布于 2024-08-06 23:49:17 字数 65 浏览 2 评论 0原文

python cgi 脚本可以向会话写入和读取数据吗?如果是这样怎么办?是否有高级 API 或者我必须推出自己的类?

Can python cgi scripts write and read data to the session? If so how? Is there a high-level API or must I roll my own classes?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

被翻牌 2024-08-13 23:49:17

cgi 上没有“会话”。如果您使用原始 cgi,则必须滚动自己的会话处理代码。

基本上,会话的工作原理是创建一个唯一的 cookie 编号并将其通过响应标头发送给客户端,然后在每个连接上检查此 cookie。将会话数据存储在服务器上的某个位置(内存、数据库、磁盘),并使用 cookie 编号作为密钥在客户端发出的每个请求时检索它。

然而,cgi 并不是使用 Python 开发 Web 应用程序的方式。使用wsgi。使用网络框架。

下面是一个使用 cherrypy 的简单示例。 cherrypy.tools.sessions 是一个cherrypy工具,它自动处理cookie设置/检索以及与数据的关联:

import cherrypy

class HelloSessionWorld(object):
    @cherrypy.tools.sessions()
    def index(self):
        if 'data' in cherrypy.session:
            return "You have a cookie! It says: %r" % cherrypy.session['data']
        else:
            return "You don't have a cookie. <a href='getcookie'>Get one</a>."
    index.exposed = True

    @cherrypy.tools.sessions()
    def getcookie(self):
        cherrypy.session['data'] = 'Hello World'
        return "Done. Please <a href='..'>return</a> to see it"
    getcookie.exposed = True

application = cherrypy.tree.mount(HelloSessionWorld(), '/')

if __name__ == '__main__':
    cherrypy.quickstart(application)

请注意,此代码是一个wsgi应用程序,从某种意义上说,您可以将其发布到任何支持 wsgi 的网络服务器(apache 有 mod_wsgi< /代码>)。另外,cherrypy 有自己的 wsgi 服务器,因此您只需使用 python 运行代码,它将开始在 http://localhost:8080/

There's no "session" on cgi. You must roll your own session handling code if you're using raw cgi.

Basically, sessions work by creating a unique cookie number and sending it on a response header to the client, and then checking for this cookie on every connection. Store the session data somewhere on the server (memory, database, disk) and use the cookie number as a key to retrieve it on every request made by the client.

However cgi is not how you develop applications for the web in python. Use wsgi. Use a web framework.

Here's a quick example using cherrypy. cherrypy.tools.sessions is a cherrypy tool that handles cookie setting/retrieving and association with data automatically:

import cherrypy

class HelloSessionWorld(object):
    @cherrypy.tools.sessions()
    def index(self):
        if 'data' in cherrypy.session:
            return "You have a cookie! It says: %r" % cherrypy.session['data']
        else:
            return "You don't have a cookie. <a href='getcookie'>Get one</a>."
    index.exposed = True

    @cherrypy.tools.sessions()
    def getcookie(self):
        cherrypy.session['data'] = 'Hello World'
        return "Done. Please <a href='..'>return</a> to see it"
    getcookie.exposed = True

application = cherrypy.tree.mount(HelloSessionWorld(), '/')

if __name__ == '__main__':
    cherrypy.quickstart(application)

Note that this code is a wsgi application, in the sense that you can publish it to any wsgi-enabled web server (apache has mod_wsgi). Also, cherrypy has its own wsgi server, so you can just run the code with python and it will start serving on http://localhost:8080/

暮年慕年 2024-08-13 23:49:17

我的“低成本”网络托管计划不允许使用 wsgi。无法使用“mod_wsgi”apache 模块,因为它是共享 apache 服务器。我正在开发自己的课程。

为了不从零开始,我正在尝试实现此站点中提供的会话类: http://cgi.tutorial.codepoint.net/a-session-class

My 'low-cost' web hosting plan don't permit use wsgi. The 'mod_wsgi' apache module can't be used because is a shared apache server. I am developing my own class.

To not start from zero, I am experimenting the implementation of a session class available in this site: http://cgi.tutorial.codepoint.net/a-session-class

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文