Python how to save HTTP cookies

I used this part for

cj = cookielib.LWPCookieJar() cookie_support = urllib2.HTTPCookieProcessor(cj) opener = urllib2.build_opener(cookie_support, urllib2.HTTPHandler) urllib2.install_opener(opener) // ..... log in with username and password. // urllib2.urlopen() to get the stuff I need. 

Now, how to save the cookie and set the expiration dates forever, so the next time I don’t have to log in again with a username and password. Can I use urllib2.urlopen() directly?

β€œNext time” I mean that after the program finishes, when I launch a new program, I can simply reload the cookie from disk and use it

thanks a lot

+4
source share
1 answer

I highly recommend using the HTTP request library. He will handle it all for you.

http://docs.python-requests.org/en/latest/

 import requests sess = requests.session() sess.post("http://somesite.com/someform.php", data={"username": "me", "password": "pass"}) #Everything after that POST will retain the login session print sess.get("http://somesite.com/otherpage.php").text 

edit: There are many ways to save a session to disk. You can do the following:

 from requests.utils import cookiejar_from_dict as jar cookies = jar(sess.cookies) 

Then read the following documentation. You can convert it to FileCookieJar and save the cookies to a text file and then load them at the beginning of the program.

http://docs.python.org/2/library/cookielib.html#cookiejar-and-filecookiejar-objects

Alternatively, you can disassemble the dict and save this data to a file and load it using pickle.load(file) .

http://docs.python.org/2/library/pickle.html

edit 2: To handle the expiration, you can iterate over the CookieJar as follows. cj it is assumed that CookieJar is obtained in some way.

 for cookie in cj: if cookie.is_expired(): #re-attain session 

To check if any of the cookies have expired, it may be more convenient to do if any(c.is_expired() for c in cj) .

+7
source

All Articles