473,387 Members | 1,464 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 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 5874
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Grant | last post by:
Hi, I'm trying to use fopen to open a remote page. I found a page that doesn't work, and it's because the page requires cookies. Is there a way in PHP to pass cookies to a remote host? Here...
4
by: Brian Burgess | last post by:
Hi all, Anyone know of any special issues with storing cookies with ASP? I'm trying this with two browsers: One is IE 6.0 with cookies set to 'prompt'. This has been working properly as any...
3
by: Jerry Rhodes | last post by:
When I run the code below, the web server tells me that I need to enable cookies. Can anyone tell me what might be causing that? I'm trying to POST userid and password to their login web page. ...
0
by: Michael Evanchik | last post by:
Hello all, since i wanted to use ssl and its seems easy to do so with this object. Im trying to login to a webserver (aol) for this example. But for some reason, im packet sniffing with ethreal...
9
by: Michael Evanchik | last post by:
Hello all, since i wanted to use ssl and its seems easy to do so with this object. Im trying to login to a webserver (aol) for this example. But for some reason, im packet sniffing with ethreal...
1
by: Peter Row | last post by:
Hi, I have a web app, there is only really 1 aspx page (that the user never sees) the rest of the code is done in a DLL that implements HttpHandler. My app (which is a porting job from Vb6...
3
by: ReGenesis0 | last post by:
Okay, so I want to do this THANG, which involves some fairly ugly screenscraping since it's a mashup with another site that doesn't have friendly, mashable, controls. Problem- I need to log into...
1
by: rrenaud | last post by:
urllib2.build_opener happily accepts and ignores a FileCookieJar. I had a bug in my code which looked like urllib2.build_opener(func_returning_cookie_jar()) which should have been ...
2
by: Joshua Kugler | last post by:
Standard disclaimer: read, googled, read some more. If you have a link, please free free to point me there. I'm using HTTPlib to construct some functional tests for a web app we're writing. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.