X-Git-Url: http://dolda2000.com/gitweb/?a=blobdiff_plain;f=wrw%2Fsession.py;h=e7d85817700e755c4869b654526fc349f75cc139;hb=537064f6656c313c601126692d31628a85de663d;hp=273775be6a9888b1cec7b7a62335a1125053f670;hpb=afd93253fc4503eefe6ab5fea320a5f0dfc6e0e6;p=wrw.git diff --git a/wrw/session.py b/wrw/session.py index 273775b..e7d8581 100644 --- a/wrw/session.py +++ b/wrw/session.py @@ -1,5 +1,5 @@ import threading, time, pickle, random, os -import cookie, env +from . import cookie, env __all__ = ["db", "get"] @@ -11,12 +11,12 @@ def hexencode(str): def gennonce(length): nonce = "" - for i in xrange(length): + for i in range(length): nonce += chr(random.randint(0, 255)) return nonce class session(object): - def __init__(self, lock, expire = 86400 * 7): + def __init__(self, lock, expire=86400 * 7): self.id = hexencode(gennonce(16)) self.dict = {} self.lock = lock @@ -39,7 +39,7 @@ class session(object): def __getitem__(self, key): return self.dict[key] - def get(self, key, default = None): + def get(self, key, default=None): return self.dict.get(key, default) def __setitem__(self, key, value): @@ -74,7 +74,7 @@ class session(object): return "" % self.id class db(object): - def __init__(self, backdb = None, cookiename = "wrwsess", path = "/"): + def __init__(self, backdb=None, cookiename="wrwsess", path="/"): self.live = {} self.cookiename = cookiename self.path = path @@ -86,7 +86,7 @@ class db(object): def clean(self): now = int(time.time()) with self.lock: - clist = self.live.keys() + clist = list(self.live.keys()) for sessid in clist: with self.lock: try: @@ -168,7 +168,9 @@ class db(object): return session(threading.RLock()) def mkcookie(self, req, sess): - cookie.add(req, self.cookiename, sess.id, path=self.path) + cookie.add(req, self.cookiename, sess.id, + path=self.path, + expires=cookie.cdate(time.time() + sess.expire)) def fetch(self, req): now = int(time.time()) @@ -202,7 +204,7 @@ class db(object): data = self.backdb[sessid] try: return pickle.loads(data) - except Exception, e: + except: raise KeyError() def freeze(self, sess): @@ -233,7 +235,7 @@ class dirback(object): with open(os.path.join(self.path, key), "w") as out: out.write(value) -default = env.var(db(backdb = dirback(os.path.join("/tmp", "wrwsess-" + str(os.getuid()))))) +default = env.var(db(backdb=dirback(os.path.join("/tmp", "wrwsess-" + str(os.getuid()))))) def get(req): return default.val.get(req)