【问题标题】:How to use sessions in cherrypy?如何在cherrypy中使用会话?
【发布时间】:2014-04-07 18:12:32
【问题描述】:

我尝试以下方法:

import cherrypy
#from cherrypy.lib import sessions

class HelloWorld(object):

    @cherrypy.expose
    def default(self, *args, **kwargs):
        out = ''
        for key, value in kwargs.items():
            out += key + '=' + value + '\n'
            cherrypy.session[key] = value
        print cherrypy.session
        return out

cherrypy.quickstart(HelloWorld())

结果我得到了

AttributeError: 'module' object has no attribute 'session'

我做错了什么?我尝试使用和不使用from cherrypy.lib import sessions。它在这两种情况下都不起作用。

【问题讨论】:

  • 试试cherrypy.config.update({"tools.sessions.on": True})

标签: python session web cherrypy


【解决方案1】:

你需要设置一些设置试试这个...

import cherrypy
#from cherrypy.lib import sessions

class HelloWorld(object):

    @cherrypy.expose
    def default(self, *args, **kwargs):
        out = ''
        for key, value in kwargs.items():
            out += key + '=' + value + '\n'
            cherrypy.session[key] = value

        #you'll also need to store a value in session
        cherrypy.session['Something'] = 'asdf'

        print(cherrypy.session.id)
        return out

cherrypy.config.update({'tools.sessions.on': True,
                        'tools.sessions.storage_type': "File",
                        'tools.sessions.storage_path': 'sessions',
                        'tools.sessions.timeout': 10
               })

cherrypy.quickstart(HelloWorld())

希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 2010-10-17
    • 2013-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多