Web client

piet at cs.uu.nl piet at cs.uu.nl
Thu Oct 26 04:54:42 EDT 2000


>>>>> piet at cs.uu.nl (P) writes:

P> This cookie module is voor the server-side not for the client side.
P> I did some code for client side cookies not too long ago, but I cannot find
P> it now. It may be at home, I'll have a look into it. It is quite simple,
P> not even a separate module.

Here is the code. It is for accessing the translation facility from
altavista.com. the first time you access that page
(http://babel.altavista.com/translate.dyn) you get a cookie. Then you
supply the cookie if you want to translate a HTML page. In this case the
cookie is given as cookie in the headers, and also in the `form'.

The standard urllib.urlopen  doesn't have a facility to specify additional
headers in the request, so I coded my own urlopen. Maybe the new urllib2
module in Python 2.0 has this facility, but I haven's seen that yet, and
there is no doc.

from urllib import *
from mimetools import Message

def urlopen(url, data=None, headers=None):
    u = FancyURLopener()
    if headers:
        if type(headers) == type({}):
            headers = headers.items()
        for kw, val in headers:
            u.addheader(kw, val)
    return u.open(url, data)

babel = "http://babel.altavista.com/translate.dyn"

form = {'doit':'done',
	'BabelFishFrontPage':'yes',
	'bblType':'url',
	'url':'http://www.cs.uu.nl/',
	'lp':'en_fr'}

start = urlopen(babel)
headers = start.info()
message = start.read()
#print message

cookie = headers.getheader("set-cookie")

print cookie
clist = string.split(cookie, ';')
print clist
session=string.split(clist[0],'=')[1]
print session

page = urlopen(babel+';$sessionid$'+session,
               urlencode(form),
               [('Cookie', clist[0]),('Referer', babel)])
print page.info()
out = open('transout.html','w')
out.write(page.read())
out.close()

-- 
Piet van Oostrum <piet at cs.uu.nl>
URL: http://www.cs.uu.nl/~piet [PGP]
Private email: P.van.Oostrum at hccnet.nl



More information about the Python-list mailing list