[mod_python] Persistent variable?

nineclue at bigfoot.com nineclue at bigfoot.com
Sat Apr 14 11:07:12 EST 2001


Greetings...

I'm trying to implement custom session handler like PHP.
I think persistent variables that share between subinterpreters would
be nice to hold session variables.
When I tried, I think persistent variables are not shared to
subinterpreter.

Sorry for my poor English... And thanks in advance for any comment or
answer.

Suhku Huh

Followings are my custom handler.

from mod_python import apache
from base64 import decodestring, encodestring
from cPickle import loads, dumps
import pg

PG_DB = 'pyweb'
_SESSION_PATH = ( '/cluelabs', )
_MODPYTHONID = 'MODPYTHONID'

session_data = {}

class session_var:
	def __init__(self, con, id = None):
		self.con = con
		if id:
			self.new = 0
			r = self.con.query("select val from session where sess_id = '%s'" % id)
			if not r.ntuples():
				self.data = {}
			else:
				val = r.getresult()[0][0]
				self.data = loads(decodestring(val))
		else:
			import md5, time

			self.new = 1
			id =  md5.new(str(time.time())).hexdigest()
			self.data = {}
		self.id = id
	def __del__(self):
		if len(self.data) == 0:
			return
		self.save()
	def __getitem__(self, name):
		if self.data.has_key(name):
			return self.data[name]
		else:
			return None
	def __setitem__(self, name, value):
		self.data[name] = value
	def __delitem__(self, name):
		try:
			del self.name[name]
		except:
			pass
	def __len__(self):
		return len(self.data)
	def has_key(self, name):
		return self.data.has_key(name)
	def save(self):
		if self.new:
			self.con.query("insert into session values ('%s', '%s')" % (self.id, encodestring(dumps(self.data))))
		else:
			self.con.query("update session set val = '%s', create_time = current_timestamp where sess_id = '%s'" % (encodestring(dumps(self.data)), self.id))
		
def _get_cookie(req):
	if req.headers_in.has_key('Cookie'):
		ret = {}
		cookies = req.headers_in['Cookie'].split(';')
		for cookie in cookies:
			equal_pos = cookie.index('=')
			key, value = cookie[:equal_pos], cookie[equal_pos+1:]
			ret[key.strip()] = value.strip()
		return ret
	else:
		return None

def _start_session(req, con):
	global session_data

	py_log = open('/home/nineclue/py_log', 'a')
	py_log.write('Session_data length : %d\n' % len(session_data))
	py_log.close()
	if req.cookie and req.cookie.has_key(_MODPYTHONID):
		req.session_var = session_data[req.cookie[_MODPYTHONID]]
	else:
		req.session_var = session_var(con)
		req.headers_out.add('Set-Cookie', '%s = %s' % (_MODPYTHONID, req.session_var.id))
		req.send_http_header()
		session_data[req.session_var.id] = req.session_var

def _get_con():
	global pg_con

	try:
		con = pg_con
	except NameError:
		pg_con = pg.connect(PG_DB)
		con = pg_con
	
	return con

def handler(req):
	global session_data

	if req.cookie:
		for key in req.cookie.keys():
			req.write('Cookie (%s) : (%s)<BR>' % (key, req.cookie[key]))
	req.write('Session Variable has %d items' % len(session_data))
	if req.session_var.has_key('count'):
		req.session_var['count'] = req.session_var['count'] + 1
	else:
		req.session_var['count'] = 0
	req.write('Counter : %02d' % req.session_var['count'])
	return apache.OK

def headerparserhandler(req):
	name_split = req.uri.rfind('/')
	req.path, req.pname = req.uri[:name_split], req.uri[name_split + 1:]

	req.cookie = _get_cookie(req)
	if req.path in _SESSION_PATH:
		con = _get_con()
		_start_session(req, con)

	return apache.OK

When I try, counters are correctly increased at first time but Key error
is raised after and py_log (used at _start_session function) shows length
of session_data is 0.

I'm using Apache 1.3.19 with statistically compiled mod_python 2.7.2
Python/2.0 (thread enabled) in Mandrake 7.1.
 




More information about the Mod_python mailing list