views:

576

answers:

2
import cgitb
import Cookie, urllib2
from cookielib import FileCookieJar
cgitb.enable()
c = Cookie.SmartCookie()
c['ini'] = 1
savedc = FileCookieJar()
savedc.add_cookie_header(c.output())
savedc.save()

shoulden't this save the cookie?...

i've been reading over the python documentation like 1 million times, i just don't get it :(

please help someone :(

A: 

Make sure you name the file to store cookies in:

savedc = FileCookieJar('cookies.txt')

add_cookie_header takes a Request object; set_cookie takes a Cookie. As it says in the documentation, FileCookieJar.save "raises NotImplementedError. Subclasses may leave this method unimplemented." Guess you should have tried reading the documentation 1E6+1 times.

outis
ouch... your last sentence about the documentation doesn't sound too friendly
Jarret Hardie
it still doesn't seem to be saving though?... when i open and close mozzila firefox the cookie is disapered?..
+3  A: 

Raf, all I can say is, Egads! The documentation certainly is not clear! I have used Python for years and this simple Stack Overflow question that I thought I'd quickly nab before getting started on real work for the day has taken me more than twenty minutes to answer. :-)

First: it turns out that the "Cookie" library and the "cookielib" library are completely separate and have nothing to do with each other. This is stated in the documentation, but you have to scroll down to the "See Also" section of each documentation page to find this out. It would be helpful if this were at the top of each page instead.

So, when you pass an object from the "Cookie" library into "cookielib", you're confusing the "cookielib" internals because it stores cookies inside of dictionaries and a "Cookie" cookie looks like — guess what! — a dictionary, so "cookielib" confuses it for one of its own internal data structures and saves other cookies inside of it. The error I get as a result is:

<type 'exceptions.AttributeError'>: 'str' object has no attribute 'discard'
  args = ("'str' object has no attribute 'discard'",)
  message = "'str' object has no attribute 'discard'"

Actually, that's the error I get after sticking a bunch of attributes on the Cookie.Cookie object that don't belong there, but that I added before I realized that I was engaged in the hopeless task of trying to get a Cookie.Cookie to behave like a cookielib.Cookie. :-) The earlier errors were all attribute-missing errors like:

<class 'Cookie.CookieError'>: Invalid Attribute name
  args = ('Invalid Attribute name',)
  message = 'Invalid Attribute name'

(And I'm putting the errors here in case some poor future soul mixes up the Cookie classes and does the Google searches I just did, none of which turned up any results for the errors I was getting!)

So before we proceed farther, I have to know: are you trying to act like a web server, delivering cookies to clients and trying to get them back intact when the client sends their next request, in which case I should show you how the "Cookie" module works? Or are you writing a web client, for testing or for fun, that messes with the cookies that it sends with a web request to a web site, in which case we should talk about "cookielib"?

Brandon Craig Rhodes
Wow... fantastic answer.
Jarret Hardie