MongoSessions / mongosessions /

import cherrypy
from pymongo import Connection

class MongoSession(cherrypy.lib.sessions.Session):
    def __init__(self, id, **kw):
        port = kw.get('port', 27017)
        host = kw.get('host', '')
        self._dbname = kw.get('dbname', 'cp_session_db')
        self._collection = kw.get('collection', 'cp_sessions')
        self._conn = Connection(host, port)
        cherrypy.lib.sessions.Session.__init__(self, id, **kw)

    def acquire_lock(self):
        self.locked = True
        print 'locked'        
    def release_lock(self):
        self.locked = False
        print 'unlocked'

    def _get_store(self):
        return self._conn[self._dbname][self._collection]
    db = property(_get_store)

    def _load(self):
        doc = self.db.find_one({'session_id':})
        if doc:
            return doc['session_data'], doc['expiration_time']
        return None

    def _save(self, expiration_time):
        print 'calling save', self._data
        doc = {
            'session_data': self._data,
            'expiration_time': expiration_time,
        doc = self.db.update({'session_id':}, doc, True)
        doc = self.db.find_one({'session_id':})
        print doc

    def _delete(self):

cherrypy.lib.sessions.MongoSession = MongoSession

if __name__ == '__main__':

    from textwrap import dedent
    from pprint import pformat

    class SampleApp(object):
        def index(self):
            cherrypy.session['info'] = {
                'foo': 'bar',
                'user_agent': cherrypy.request.headers['User-Agent']
            return dedent('''
                <head><title>test it!</title></head>
                          <a href="/sess_value">Check out this session info</a>
                <hr />

        def sess_value(self):
            cherrypy.response.headers['Content-Type'] = 'text/plain'
            return pformat(cherrypy.session['info'])

    config = {
        '/': {
            'tools.sessions.on' : True ,
            'tools.sessions.storage_type'  : 'mongo',
            '' : 'MongoSes',

        'server.socket_port': 9000,
        'server.socket_host': '',
    cherrypy.tree.mount(SampleApp(), script_name='/', config=config)



Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.