[Tutor] urllib & client side cookies

Charlie Clark charlie@begeistert.org
Tue, 12 Mar 2002 23:54:00 +0100


s far as I've so far been able to gather, Python's cookie module only really supports server-side cookies. But my current project requires that I set a client-side cookie.

Google came up with a very good presentation by Moshe Zadke at Python 9
http://www.python9.org/p9-zadka.ppt
or in staight HTML
http://www.google.de/search?q=cache:CZr5DdjK6zwC:www.python9.org/p9-zadka.ppt

which includes a couple of examples which I've tried to learn from:

The example source is:

import urllib, string 
agent = urllib.URLopener() 
d = {
         'u':  'moshez',
         'pass': 'not my real password'
} 
u = agent.open('http://advogato.org/acct/loginsub.html',
               urllib.urlencode(d))
cookie = u.info()['set-cookie']
cookie = cookies[string.find(cookie, ';')]
agent.addheader('Cookie', cookie)
d = {
         'entry': open('entry').read(),
         'post': 'Post'
} 
u = agent.open('http://advogato.org/diary/post.html',
                         urllib.urlencode(d))

This won't run out of the box, not just because of the invalid Password but also because of typo.
line 7 should read:
cookies = u.info()['set-cookie']
line 8 had me foxed for a while as it, too
cookie = cookies[:cookie.find(';')]
this is a not so obvious slice = cookies.split(";")[0] which is more readable for me

Oh, well now that those little problems are over. Does it work?
Well, it does get my cookie for me but I don't seem to be able to use use the cookie correctly. This is great because it means I can get some work done without having to become an expert on http first.

Steve Purcell has already made the web-unit module available for working with cookies but it seems to require more detailed knowledge of http than i currently have :-(.

Here's my script

import urllib
agent = urllib.URLopener()

u = agent.open("http://localhost/login.jsp")
cookies = u.info()['set-cookie']

cookie = cookie.split(';')[0]
cookie = cookie.lower()
# need the cookie to be lowercase for some reason
print cookie
agent.addheader('Cookie', cookie)

d = {'page':'firstpage', 'password':'my_password'} 
##u = agent.open("http://localhist/login_trx.jsp",urllib.urlencode(d))
u = agent.open("http://localhist/login_trx.jsp?page=16password=my_password;" + cookie)
content = u.read()

This does get me my cookie which I was able to test in a brower session but my python script gives me the following error:

JSESSIONID=ke334pszf1
[('User-agent', 'Python-urllib/1.15')]
Traceback (most recent call last):
  File "D:\spider\cookie_test.py", line 19, in ?
    u = agent.open("http://192.168.103.26/login_trx.jsp?institution=3&kennwort=preentry;" + cookie)
  File "C:\Python21\lib\urllib.py", line 176, in open
    return getattr(self, name)(url)
  File "C:\Python21\lib\urllib.py", line 296, in open_http
    return self.http_error(url, fp, errcode, errmsg, headers)
  File "C:\Python21\lib\urllib.py", line 313, in http_error
    return self.http_error_default(url, fp, errcode, errmsg, headers)
  File "C:\Python21\lib\urllib.py", line 319, in http_error_default
    raise IOError, ('http error', errcode, errmsg, headers)
IOError: ('http error', 302, 'Found', <mimetools.Message instance at 00A6D2D4>)

I'm not quite sure what the error message means which I'm getting back. I assume I'm getting something sent back by the server which I need to process but I think I might also not quite be sending the cookie in the right form.

I'm very much appreciate any help on getting this sorted. It's all part of the usual Sisyphus work but still has to get done.

Charlie