-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsession.py
executable file
·82 lines (65 loc) · 2.6 KB
/
session.py
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
#!/usr/bin/python
# -*- coding: iso-8859-1 -*-
####
# Copyright (C) 2006 Clodoaldo Pinto Neto [email protected]
#
# 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 script 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 (www.gnu.org) for more details.
#
# You can retrieve a copy of the GNU General Public License
# from http://www.gnu.org/. For a copy via US Mail, write to the
# Free Software Foundation, Inc.
# 59 Temple Place - Suite 330,
# Boston, MA 02111-1307
# USA
####
import sha, shelve, time, Cookie, os
class Session(object):
def __init__(self, expires=None, cookie_path=None):
string_cookie = os.environ.get('HTTP_COOKIE', '')
self.cookie = Cookie.SimpleCookie()
self.cookie.load(string_cookie)
if self.cookie.get('sid'):
sid = self.cookie['sid'].value
# Clear session cookie from other cookies
self.cookie.clear()
else:
self.cookie.clear()
sid = sha.new(repr(time.time())).hexdigest()
self.cookie['sid'] = sid
if cookie_path:
self.cookie['sid']['path'] = cookie_path
#session_dir = os.environ['DOCUMENT_ROOT'] + '/elizia/session'
session_dir = 'session'
#print session_dir
if not os.path.exists(session_dir):
#if not os.path.exists("session"):
try:
os.mkdir(session_dir, 02770)
#os.mkdir("session", 02770)
# If the apache user can't create it create it manualy
except OSError, e:
errmsg = """%s when trying to create the session directory. \
Create it as '%s'""" % (e.strerror, os.path.abspath(session_dir))
raise OSError, errmsg
self.data = shelve.open(session_dir + '/sess_' + sid, writeback=True)
os.chmod(session_dir + '/sess_' + sid, 0660)
# Initializes the expires data
if not self.data.get('cookie'):
self.data['cookie'] = {'expires':''}
self.set_expires(expires)
def close(self):
self.data.close()
def set_expires(self, expires=None):
if expires == '':
self.data['cookie']['expires'] = ''
elif isinstance(expires, int):
self.data['cookie']['expires'] = expires
self.cookie['sid']['expires'] = self.data['cookie']['expires']