如何在cherrypy中使用会话?

时间:2014-04-07 18:12:32

标签: python session web cherrypy

我尝试以下方法:

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的情况下尝试使用它。它在两种情况下都不起作用。

1 个答案:

答案 0 :(得分:3)

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

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())

希望这有帮助!