[Tutor] Fun with Cookies

Kent Johnson kent37 at tds.net
Fri Sep 7 20:58:35 CEST 2007


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