[mod_python] Session always create new session ID

Martin Slouf xslom03 at vse.cz
Sun Oct 3 09:55:05 EDT 2004


Hi,

look at the code listening bellow, you have to call session.save() (handler
request) manually at end of each request.  maybe using another handler
(mod_python.servlet -- http://home.comcast.net/~d.popowich/mpservlets/) is more
convenient.

Also, session id is stored as a cookie (pysid) on client side, so maybe no need
to print it each time into each link element.

But there is something else id like to know -- is there a way how can i rename
the default name ('pysid') of a session -- ie. if im using 2 sites on my server
which both use session, there will be a problem.

reagrds, martin.

(možná můžu i česky? :)

- - - - 

#! /usr/bin/env python
# -*- ISO-8859-2 -*-

# Time-stamp: < session_test.py (15:20 25.9.2004) >

from mod_python import apache
from mod_python import Session

def index(req):
    s = Session.Session(req, timeout = 1)
    if (s.is_new()):
        s["count"] = 0
        s["tmp"] = "message"
        s["list"] = ["1", "b", "2", "d"]
        s["square"] = Square(5)
    s["count"] += 1
    req.write(s.id() + "\n")
    req.write(str(s) + "\n")
    req.write(str(s["square"].a) + "\n")
    s.save()

def invalidate(req):
    s = Session.Session(req)
    s.invalidate()
    req.write("invalidate")

class Square:
    def __init__(self, a = 0):
        self.a = 0
    def __str__(self):
        return "Square: a = %d" % (self.a,)
    def __repr__(self):
        return str(self)

- - - -

# .htacces

AddHandler mod_python .py
PythonHandler mod_python.publisher
PythonDebug On
PythonAutoReload On

- - - -

On Sun, Oct 03, 2004 at 01:00:31AM +0200, Lukas Linhart wrote:
> Greetings, 
> 
> I'm trying to use session management with mod_python.publisher. I'm testing 
> following code: 
> 
> def index(req, sessID):
>  req.content_type = "text/html"
>  if not sessID:
>   req.write("New session")
>   relace = Session.Session(req)
>  else:
>   req.write("Restoring session "+sessID")
>   relace = Session.Session(req, sessID)
>  req.write("<a href=\"testing.py?sessID="+relace.id()+"\">Reload</a>")
> 
> 
> Well, here is no problem with creating session or printing it's ID on 
> "Reload". However, on each request Session.Session(req, sessID) generate a 
> new ID. 
> 
> What I'm doing wrong / where problem should lie? 
> 
> Regards, 



> _______________________________________________
> 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