开发者

how to use cherrpy built in data storage

Ok I have been reading the cherrypy documents for sometime and have not found a simple example yet. Let say I have a simple hello world site, how do I store data? Lets say I want to store a = 1, and b =2 to a dictionary using cherrypy. The config files are confusing as hell. Anyone have very simple example of storing values from a simple site in cherrypy?

Here is my code what am I doing wrong? I made 开发者_JS百科a tmp file c:/tmp, where is the config file, and or where do I put it? This code worked before I try adding config?

import cherrypy
import os

cherrypy.config.update({'tools.sessions.on': True,
 'tools.sessions.storage_type': "file",
 'tools.sessions.storage_path': "/tmp",
 'tools.sessions.timeout': 60})

class Application:

    def hello(self,what='Hello', who='world'):
        cherrypy.session['a'] = 1
        return '%s, %s!' % (what, who)

    hello.explose=True
root = Application()
cherrypy.quickstart(root)


Edit your config file:

[/]
tools.sessions.on = True
tools.sessions.storage_type = "file" # leave blank for in-memory
tools.sessions.storage_path = "/home/site/sessions"
tools.sessions.timeout = 60

Setting data on a session:

cherrypy.session['fieldname'] = 'fieldvalue'

Getting data:

cherrypy.session.get('fieldname')

Source: http://www.cherrypy.org/wiki/CherryPySessions


You configure cherrypy to use sessions and store them to a file, e.g. in this way:

 cherrypy.config.update({'tools.sessions.on': True,
     'tools.sessions.storage_type': "file",
     'tools.sessions.storage_path': "/tmp/cherrypy_mysessions",
     'tools.sessions.timeout': 60})

(or similarly in the config file of course), then cherrypy.session is the "per-user" dict you want, and cherrypy.session['a'] = 1 and similarly for 'b' is how you can store data there.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜