Python 如何在cherrypy中使用会话?

Python 如何在cherrypy中使用会话?,python,session,web,cherrypy,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

我尝试以下方法:

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'

我做错了什么?我在cherrypy.lib导入会话中尝试使用和不使用
。这两种情况下都不起作用。

您需要设置一些设置。请尝试此

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

希望这有帮助

尝试
cherrypy.config.update({“tools.sessions.on”:True})