Вопрос

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. 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 с атрибуция
Не связан с StackOverflow
scroll top