[mod_python] The right way to handle sessions

Amir Salihefendic amix at amix.dk
Thu Aug 12 16:17:49 EDT 2004


Hello Fabio

Thanks for your input. But that does not work :-(
Try this:
def createSession(req):
     sess = get_session(req)
     req.write(sess.id())
     return getSameSession(req)

def getSameSession(req):
     sess = get_session(req)
     return "\n%s" % sess.id()

def get_session(req):
	sess = Session.Session(req)
	if sess.is_new():
		sess.save ()
	else:
		sess.load()
	return sess

Try to store this in a file (index.py) and call index.py/createSection
First time you run it the sid's are going to be different...
Second time you run it - - it will make an infinitive loop (or at least 
mess apache up).

The method only works if you call the two functions:
index.py/createSection
and after that you call:
index.py/getSameSection
Then you get the deserved output.

My question is more: How do you work on sessions across functions - - 
by storing data in the request (req) object or what??

Med venlig hilsen / Kind regards
Amir Salihefendic
-----
What we do in life echoes in eternity

Den 12/8-2004, kl. 14.48, skrev Fabio Rotondo:

> Amir Salihefendic wrote:
>> Hello.
>> I welcome myself to this list ;-)
>> I have a question about how to handle sessions correctly.
>> I am creating a simple login system (just messing around with 
>> mod_python).
>> Let's say that we have a page called index.py and we are using the 
>> publisher handler.
>
> Hi,
>
> try this code: ``req`` is the request object from mod_python.
>
> def get_session ( req ):
> 	sess = Session.Session  ( req )
>
> 	if sess.is_new ():
> 		sess.save ()
> 	else:
> 		sess.load ()
>
> 	return sess
>
> Ciao,
>
>   Fabio
> _______________________________________________
> Mod_python mailing list
> Mod_python at modpython.org
> http://mailman.modpython.org/mailman/listinfo/mod_python
>



More information about the Mod_python mailing list