sharedance sessions backend

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
from django.conf import settings
from sharedance.api import *
from django.contrib.sessions.backends.base import SessionBase
from django.core.exceptions import SuspiciousOperation, ImproperlyConfigured
import cjson
class SessionStore(SessionBase):
    def __init__(self, session_key=None):
        self.sharedance = shareDance('localhost',1042)
        super(SessionStore, self).__init__(session_key)

    def load(self):
        session_data = {}
        try:
            session_data = self.decode(self.sharedance.get(self.session_key))
#            session_data = cjson.decode(self.sharedance.get(self.session_key, encoding='utf-8'))
        except:
            self._session_key = self._get_new_session_key()
            self._session_cache = {}
            self.save()
            # Ensure the user is notified via a new cookie.
            self.modified = True
        return session_data

    def save(self):
        try:
            self.sharedance.set(self.session_key,self.encode(self._session))
#            self.sharedance.set(self.session_key,cjson.encode(self._session, encoding='utf-8')))
        except:
            pass

    def exists(self, session_key):
        try:
            return self.sharedance.get(session_key) != ''
        except:
            return False
        
    def delete(self, session_key):
        try:
            self.sharedance.delete(self.session_key)
        except:
            pass

    def clean(self):
        pass

------------------------------------------------------------------------------------------------------
import socket

class shareDance(object):
    def __init__(self, host, port):
        self.host = host
        self.port = port
            
    def connect(self):
        self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.socket.connect((self.host, self.port))
        
    def close(self):
        self.socket.close()
        self.socket = None
        
    def set(self, key, data):
        key_len = len(key)
        data_len = len(data)
        self.connect()
        self.socket.send('S%c%c%c%c%c%c%c%c%s%s' % (
             (key_len >> 24) & 0xff, 
             (key_len >> 16) & 0xff,
             (key_len >> 8) & 0xff, 
             key_len & 0xff,
             (data_len >> 24) & 0xff, 
             (data_len >> 16) & 0xff,
             (data_len >> 8) & 0xff, 
             data_len & 0xff, 
             key, 
             data))
        if self.socket.recv(3) == "OK\n":
            self.close()
            return True
        else:
            self.close()
            return False
        
    def get(self, key):
        key_len = len(key)
        self.connect()
        self.socket.send('F%c%c%c%c%s' % (
             (key_len >> 24) & 0xff, 
             (key_len >> 16) & 0xff,
             (key_len >> 8) & 0xff, 
             key_len & 0xff, 
             key))
        response = ''
        while True:
            data = self.socket.recv(4096)
            if not data: break
            response = response + data
        self.close()    
        return response
        
    def delete(self, key):
        key_len = len(key)
        self.connect()
        self.socket.send('F%c%c%c%c%s' % (
             (key_len >> 24) & 0xff, 
             (key_len >> 16) & 0xff,
             (key_len >> 8) & 0xff, 
             key_len & 0xff, 
             key))
        if self.socket.recv(3) == "OK\n":
            self.close()
            return True
        else:
            self.close()
            return False

More like this

  1. User from session key by jdunck 5 years, 3 months ago
  2. Test if sessions are working by jpic 4 years, 1 month ago
  3. clear session table by rubic 7 years, 1 month ago
  4. email backend which use sendmail subprocess by pawnhearts 4 years, 3 months ago
  5. RefreshSessionMiddleware by jcassee 5 years, 8 months ago

Comments

derivin (on April 12, 2008):

Now all we need is a cache backend as well

#

justinguru (on December 22, 2013):

Thanks for helpful share!

#

(Forgotten your password?)