简体   繁体   English

如何在Cherrypy中使用会话?

[英]How to use sessions in cherrypy?

I try the following: 我尝试以下方法:

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

As a result I get 结果我得到了

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

What am I doing wrong? 我究竟做错了什么? I try it with and without from cherrypy.lib import sessions . 我尝试了是否包含from cherrypy.lib import sessions It does not work in both cases. 在两种情况下均不起作用。

You'll need to set a few settings try this... 您需要设置一些设置,尝试一下...

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

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM