Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python FileCookieJar.save() issue

I have a problem while trying to save cookies to a file using FileCookieJar's save method. Here is my code:

#!/usr/bin/python
import httplib, cookielib, urllib2, json, time
from datetime import date


class FoN:
    def __init__(self):
            self.cookiefile = "cookies.txt"
            self.cj = cookielib.FileCookieJar(self.cookiefile)
    def login (self, login, password):
            js = json.JSONEncoder().encode({"login":login,"password":password})
            req=urllib2.Request("http://www.example.com/user/login", js)
            res=urllib2.urlopen(req)
            self.cj.extract_cookies(res,req)
            self.cj.save(self.cookiefile, ignore_discard=True)
            f.write ("Login: "+login+", result: "+str(res.read().count("true"))+"\n")
            time.sleep(2)
            return res

So it fails at self.cj.save(self.cookiefile, ignore_discard=True) raising NotImplementedError exception, which is according to the documentation. But my question how do I save cookies to the file then? I even tried to include the code in try clause but that didn't help at all.

like image 955
Azimuth Avatar asked Jan 13 '12 09:01

Azimuth


1 Answers

The base FileCookieJar does not implement .save To get saving, you should use one of the subclasses like MozillaCookieJar or LWPCookieJar.

like image 87
Glenn Avatar answered Oct 27 '22 11:10

Glenn