My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have receivedd a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Author: Christian Schulze <xcr4cx@googlemail.com>
# Description: Simple sessions implemented in Python

import Cookie
import os
import sys
import uuid
SESSION_PATH = os.getcwd() + '/.sessions'

class Session(dict):
def __init__(self, expire_date=None):
if SESSION_PATH:
if not os.path.exists(SESSION_PATH):
os.mkdir(SESSION_PATH)
self.sessions_path = os.getcwd() + "/.sessions"
else:
self.sessions_path = SESSION_PATH
else:
sys.stderr.write("No such SESSIONS_PATH")
sys.exit()

self.cookie = Cookie.SimpleCookie()

try:
self.cookie.load(os.environ["HTTP_COOKIE"])
self.id = self.cookie["session_id"].value
self.readSession()
except:
pass #sys.stderr.write("exception in __init__") #debug

# be really sure, self.id is not already set
try: self.id
except: self.id = uuid.uuid4()

# no session but cookie
if not os.path.exists(self.sessions_path + '/' + str(self.id)):
self.id = uuid.uuid4()

# set cookie
self.cookie["session_id"] = self.id
if expire_date:
self.cookie["session_id"]["expires"] = expire_date
print self.cookie, "\n\n"

def setSession(self):
# set session
sessions_file = open(self.sessions_path + '/' + str(self.id), 'w')
session_vars = ""
for atr in self:
session_vars += atr + ': ' + str(self[atr]) + '\n'
sessions_file.write(session_vars)

def readSession(self):
if os.path.exists(self.sessions_path + '/' + str(self.id)) and self.id:
session_file = open(self.sessions_path + '/' + str(self.id)).read()
lines = session_file.split('\n')
for line in lines:
if line != '':
line = line.split(':', 1)
atr = line[0]

while line[1].startswith(' '):
line[1] = line[1][1:]

val = line[1]
self[atr] = val
else:
sys.stderr.write("Cannot read session :" + self.id)

def __del__(self):
self.setSession()

Change log

r13 by xCr4cx on Jul 8, 2010   Diff
fixed  issue #1 
Go to: 
Project members, sign in to write a code review

Older revisions

r12 by xCr4cx on Jul 8, 2010   Diff
changed python3 to 2 - fixed low
priority bug
r10 by xCr4cx on Jul 8, 2010   Diff
first try to implement the expiration
date of the session cookie
r9 by xCr4cx on Jul 8, 2010   Diff
removed debugging stuff
All revisions of this file

File info

Size: 2988 bytes, 87 lines

File properties

svn:executable
*
Powered by Google Project Hosting