[Tutor] Fun with Cookies

Alex Ezell aezell at gmail.com
Fri Sep 7 21:48:36 CEST 2007


Kent,
Thanks so much. I will give that a try. Your name is all over these
kinds of questions on the web. I guess you fought through it a while
back?

Here's where I show off my Python newb status. What's the best way to
specify those attributes? If I only include the 4 you mention (name,
value, domain and path), it seems messy to have a bunch of "None"s in
there.

Thanks again,
Alex

On 9/7/07, Kent Johnson <kent37 at tds.net> wrote:
> Alex Ezell wrote:
> > Hi all,
> > I am trying to create a cookie and send it a long with a request.
> >
> > I searched the archives and found this code from Kent Johnson:
> >
> > import cookielib, urllib2
> >
> > cj = cookielib.CookieJar()
> > cookie = cookielib.Cookie(...your cookie data here...)
> > cj.set_cookie(cookie)
> >
> > opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
> > urllib2.install_opener(opener)
> >
> > data = urllib2.urlopen(...).read()
> >
> > It seemed to solve a problem I was having with SimpleCookie(), but I
> > cannot figure out what I should put where Kent has written "...your
> > cookie data here...". I have tried strings, SimpleCookie instances,
> > etc. and I always get this error on that line:
> >
> > __init__() takes at least 17 arguments (2 given)
>
> The Cookie constructor is
>
>      def __init__(self, version, name, value,
>                   port, port_specified,
>                   domain, domain_specified, domain_initial_dot,
>                   path, path_specified,
>                   secure,
>                   expires,
>                   discard,
>                   comment,
>                   comment_url,
>                   rest,
>                   rfc2109=False,
>                   )
>
> You should specify at least name, value, domain and path (all strings).
> The rest can be None.
>
> Kent
>


More information about the Tutor mailing list