Hi,

Denise, if you're handling cookies client side, then this is how to do it (code snippets taken from http://www.voidspace.org.uk/python/articles/cookielib.shtml as I'm at work.)

import os.path
import urllib2
import cookielib


COOKIEFILE = 'cookies.lwp'
# the path and filename to save your cookies in

urlopen
= urllib2.urlopen
Request = urllib2.Request
cj = cookielib.LWPCookieJar( )
 # This is a subclass of FileCookieJar
 
# that has useful load and save methods

if os.path.isfile (COOKIEFILE):
         # if we have a cookie file already saved
         
# then load the cookies into the Cookie Jar
        
cj .load(COOKIEFILE)

opener = urllib2.build_opener( urllib2.HTTPCookieProcessor(cj))
urllib2.install_opener(opener)

#The above two lines initialise a opener which can handle cookies, and will use our cookie jar

theurl = 'http://www.google.co.uk/search?hl=en&ie=UTF-8&q=voidspace&meta= '
# an example url that sets a cookie,
# try different urls here and see the cookie collection you can make !

txdata = None
# if we were making a POST type request,
# we could encode a dictionary of values here,
# using urllib.urlencode(somedict)

txheaders =  {'User-agent' : 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'}
# fake a user agent, some websites (like google) don't like automated exploration

try:
    req = Request(theurl, txdata, txheaders)
    # create a request object

    handle = urlopen(req)
    # and open it to return a handle on the url

except IOError, e:
    print 'We failed to open "%s".' % theurl
    if hasattr(e, 'code' ):
        print 'We failed with error code - %s.' % e. code
    elif hasattr(e, 'reason' ):
        print "The error object has the following 'reason' attribute :"
        print e.reason
        print "This usually means the server doesn't exist,',
        print "is down, or we don't have an internet connection."
    sys.exit()

else:
    print 'Here are the headers of the page :'
    print handle.info()
    # handle.read() returns the page
    # handle.geturl() returns the true url of the page fetched
    # (in case urlopen has followed any redirects, which it sometimes does)

print
if cj is None:
    print "We don't have a cookie library available - sorry."
    print "I can't show you any cookies."
else:
    print 'These are the cookies we have received so far :'
    for index, cookie in enumerate (cj):
        print index, '  :  ', cookie
    cj.save(COOKIEFILE) 



-----

Phew! A bit of code, but that shows a simple usage(!) of it.

Good luck.

On 7/5/05, Kent Johnson <[EMAIL PROTECTED]> wrote:
Danny Yoo wrote:
> To make cookies, in the examples of the Cookie module will probably help
> the most:
>
>     http://www.python.org/doc/lib/cookie-example.html
>
>>From the documentation, it sounds like Cookie.SimpleCookie is what you're
> looking for:

My understanding is that the Cookie module is for server-side cookie handling. cookielib.Cookie integrates with cookielib.CookieJar for client-side cookie handling which is what Denise is looking for. Though possibly I am missing something...

Kent

_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor



--
'There is only one basic human right, and that is to do as you damn well please.
And with it comes the only basic human duty, to take the consequences.'
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to