471,075 Members | 856 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,075 software developers and data experts.

Cookies and CookieJar

I'm struggling with a project using mechanize and cookies to screen scape a
website. The site requires a client created cookie for authentication. Below
is the code I'm attempting to use with the traceback I'm getting:
>>import Cookie
c=Cookie.SimpleCookie()
c["Manageopen"]="cards"
c['Manageopen']['expires'] = 0
c['Manageopen']['path'] = "/"
c['Manageopen']['domain'] = ".domain.com"
c['Manageopen']['secure'] = ""
c.output()
'Set-Cookie: Manageopen=cards; Domain=.domain.com; expires=Fri, 16-May-2008
14:06:00 GMT; Path=/'
>>import cookielib
cj=cookielib.CookieJar()
cj.set_cookie(c)
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
File "C:\Python25\lib\cookielib.py", line 1617, in set_cookie
if cookie.domain not in c: c[cookie.domain] = {}
AttributeError: 'SimpleCookie' object has no attribute 'domain'
>>>
I also tried:
>>cj.set_cookie(c["Manageopen"])
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
File "C:\Python25\lib\cookielib.py", line 1617, in set_cookie
if cookie.domain not in c: c[cookie.domain] = {}
AttributeError: 'Morsel' object has no attribute 'domain'
I've looked at everything I can find via Google and nothing seems to help.

Thanks in advance.

Regards,
Larry
Jun 27 '08 #1
2 5443
On May 16, 9:21*am, Larry Bates <larry.ba...@websafe.com`wrote:
I'm struggling with a project using mechanize and cookies to screen scape a
website. *The site requires a client created cookie for authentication. *Below
is the code I'm attempting to use with the traceback I'm getting:

*>>import Cookie
*>>c=Cookie.SimpleCookie()
*>>c["Manageopen"]="cards"
*>>c['Manageopen']['expires'] = 0
*>>c['Manageopen']['path'] = "/"
*>>c['Manageopen']['domain'] = ".domain.com"
*>>c['Manageopen']['secure'] = ""
*>>c.output()
'Set-Cookie: Manageopen=cards; Domain=.domain.com; expires=Fri, 16-May-2008
14:06:00 GMT; Path=/'
*>>import cookielib
*>>cj=cookielib.CookieJar()
*>>cj.set_cookie(c)
Traceback (most recent call last):
* *File "<interactive input>", line 1, in <module>
* *File "C:\Python25\lib\cookielib.py", line 1617, in set_cookie
* * *if cookie.domain not in c: c[cookie.domain] = {}
AttributeError: 'SimpleCookie' object has no attribute 'domain'
*>>>

I also tried:

*>>cj.set_cookie(c["Manageopen"])
Traceback (most recent call last):
* *File "<interactive input>", line 1, in <module>
* *File "C:\Python25\lib\cookielib.py", line 1617, in set_cookie
* * *if cookie.domain not in c: c[cookie.domain] = {}
AttributeError: 'Morsel' object has no attribute 'domain'

I've looked at everything I can find via Google and nothing seems to help.

Thanks in advance.

Regards,
Larry
Hi,

In the python docs, set_cookie() is defined like this:

---
set_cookie(cookie)
Set a Cookie, without checking with policy to see whether or not it
should be set.
---

The Cookie mentioned there is a an object of the Cookie class which is
defined in the cookielib module. On the other hand,
Cookie.SimpleCookie() creates an object of a class called SimpleCookie
(and SimpleCookie inherits from a class called BaseCookie).
Therefore, the object returned by Cookie.SimpleCookie() is not a
Cookie--it's a SimpleCookie. Just because the module that contains
the SimpleCookie class is called Cookie does not mean that
SimpleCookie inherits from the Cookie class or that SimpleCookie has
any connection to the Cookie class located in the cookielib module.
If that's too confusing, here is the way the python modules are set
up:

Cookie module:
-------------
BaseCookie class
|
V
SimpleCookie class


cookielib module
----------------
Cookie class

CookieJar class
---set_cookie(Cookie)
What you did in your code was you supplied set_cookie() with a
SimpleCookie object, and set_cookie() expects a Cookie object.
SimpleCookie objects are a mapping and you access the data using the
notation:
*>>c['Manageopen']['domain'] = ".domain.com"
But if you open up cookilib.py and look at the line in the error
message:
File "C:\Python25\lib\cookielib.py", line 1617, in set_cookie
you will see that the code uses the notation:
* * *if cookie.domain ...
That's because the Cookie class defines simple objects that have
attributes that are accessed normally, i.e. with dot notation.

The site requires a client created cookie for authentication.
A "cookie" is just one of the headers in a request. Instead of using
a url string to retrieve a web page with urllib2.urlopen(), you can
use a Request object. And Request objects allow you to set headers,
which means you can set a cookie header.

But the site will probably check every subsequent request for that
cookie header as well, so that means you will need to add the header
to every subsequent request you make. That is where CookieJar can
help you. It can store cookies in a file and then automatically add
them to every request for you. It's a bit complicated though. See
here:

http://www.voidspace.org.uk/python/a...ookielib.shtml




Jun 27 '08 #2
Larry Bates wrote:
I'm struggling with a project using mechanize and cookies to screen scape a
website. The site requires a client created cookie for authentication.
Are you sure that is correct? As far as I know, the usual course of
events is for a site to set a cookie header in the response it sends
back to the client browser, which gets stored on the client's
computer. Then when the browser makes subsequent requests to that
site, the browser will automatically include the cookies that were set
by that site.
Jun 27 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Grant | last post: by
4 posts views Thread by Brian Burgess | last post: by
3 posts views Thread by Jerry Rhodes | last post: by
reply views Thread by Michael Evanchik | last post: by
9 posts views Thread by Michael Evanchik | last post: by
1 post views Thread by Peter Row | last post: by
3 posts views Thread by ReGenesis0 | last post: by
2 posts views Thread by Joshua Kugler | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.