[mod_python] servlets and cookies

John Aherne johna at johnaherne.co.uk
Sun Oct 10 11:35:35 EDT 2004


I am new to all this.

I have installed and run modpython on windows2kserver with python2.3.

I have run the examples and they all work fine. I used the publisher 
interface which was straightforward.

I then looked at PSP and that was fine.

Then I found mpsservlets and that looked much better. I really liked the 
way it went about things. So I started to play around with that.

The examples are fine. They have useful code samples to copy and 
incorporate.

However, I am having a problem using cookies. Whatever I do I can't seem 
to get them to work.
So I looked at the sessions example. I took some of the code from the 
servlet example and of course that worked fine. I could pick up the 
session id whereas with cookies I could not get to see my stored cookie.

The feeling I get is that my add_cookie method is not working, since 
with the session I can get at my session id.

But the add_cookie method is so simple what could I be doing wrong.

I have 3 servlets. index, login, database display.

I start with index. If not logged in as correct user, using 
self.forms.getfirst, I redirect to login using self.external_redirect.

Login posts back to index which checks the username with 
self.forms.getfirst. If correct username logged in I then set a cookie 
and self.external_redirect to database display.

Database display reads the cookie. If present it will display the data. 
It never seems to find the cookie.

The code in index to add cookie is:

check = Cookie('user', 'myname')
add_cookie(self.req, check)

The code to read the cookie is:-

checkout = get_cookies(self.req, Cookie('user', 'myname')
if checkout.has_key('user'):
etc...

I am assuming that the self.external_redirect is the correct thing to do 
to force the client to resend so the cookie is sent back. Session id 
seems happy with this method.( one caveat, the first time it moans about 
'pysid' since I writeln out the session id to check it is the right one- 
but I am ignoring that for now while I concentrate on the cookies problem).

If anyone can spot the obvious flaw in my methodology above I would be 
grateful for any help as to what I have done wrong. It must be something 
pretty dumb since the add_cookie method doesn't offer much scope for 
doing anything wrong.

Thanks for any help.

John Aherne






More information about the Mod_python mailing list