David Fraser
davidf at sjsoft.com
Fri Apr 4 08:21:06 EST 2003
Ron Wills wrote: > Hello All > > ~ There's something that I can't seem to find the in the docs. Currently > I have a set of PHP libs that I use, but was hoping to port my > authenication classes to mod_python. This could simplify things for me, > but I can seem to find anything to do session tracking (with cookies or > so on). Any help would be appretiated. > I've just done this for our application for mod_python. Basically we use the getcookie and setcookie functions below to get and set the cookie Then we have session objects, each of which has a unique session string, which is set in the cookie. We have a global sessioncache dictionary that looks up a session object based on the session string. We also use this for handling user login etc, so its a bit more complicated, but that's the basic idea and you should be able to use the same idea. When doing this, I was wondering whether some cookie methods could be added to the request object, since that's where they seem to belong? Could pass in the class of cookie you want to use etc... Any thoughts? David def getcookie(req): """Reads in any cookie info from the request""" try: cookiestring = req.headers_in["Cookie"] except: cookiestring = {} cookie = Cookie.SimpleCookie() cookie.load(cookiestring) cookiedict = {} for key, morsel in cookie.iteritems(): cookiedict[key] = morsel.value return cookiedict def setcookie(req, cookiedict): """Puts the bits from the cookiedict into Morsels, sets the req cookie""" # construct the cookie from the cookiedict cookie = Cookie.SimpleCookie() for key, value in cookiedict.iteritems(): cookie[key] = value # add the cookie headers to req.headers_out for key, morsel in cookie.iteritems(): req.headers_out.add('Set-Cookie', morsel.OutputString())
|