[Tutor] I sure do love cookies.

D. Hartley denise.hartley at gmail.com
Tue Jul 5 21:07:37 CEST 2005


Thank you for the code, everyone.

I actually have a piece of information (something like
"this+is+a+cookie") that I am trying to *send* (not receive), and I'm
not sure how to do it.  I looked at the Cookie examples a little bit,
but am having trouble applying what I see there to my present
situation, since there is so much that either a). doesnt apply or b).
is over my head (usually c). all of the above!). Although the
client-side illustration you provided was very clear and I'll archive
that for future use, too.

So, for *sending* cookies, it seems that I would want to create one this way:

######
>>> import Cookie
>>> mycookie = Cookie.SimpleCookie()
>>> mycookie['value'] = 'this+is+a+cookie'
>>>
>>> mycookie
<SimpleCookie: value='this+is+a+cookie'>
>>>
>>> print mycookie
Set-Cookie: value=this+is+a+cookie;
######

But then what? How do I send this anywhere? Right now it seems to be
just a thing sitting in my python screen, and not something that
actually does anything. If I have correctly created a cookie (have
I?), what do I do with it now?

Thanks,
Denise

On 7/4/05, Liam Clarke <cyresse at gmail.com> wrote:
> 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 <kent37 at tds.net> 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 at 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 at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 
> 
>


More information about the Tutor mailing list