How to keep cookies when making http requests (Python 2.7)

dieter dieter at handshake.de
Wed Aug 21 02:07:35 EDT 2013


Luca Cerone <luca.cerone at gmail.com> writes:
> ...

Python has a module for cookie handling: "cookielib" ("cookiejar"
in Python 3).

"urllib2" has a standard way to integrate with this module.
However, I do not know the details (check the documentation
for the modules).

I have used "cookielib" externally to "urllib2". It looks
like this:

from urllib2 import urlopen, Request
from cookielib import CookieJar

cookies = CookieJar()
....
r = Request(...)
cookies.add_cookie_header(r) # set the cookies
R = urlopen(r, ...) # make the request
cookies.extract_cookies(R, r) # remember the new cookies





More information about the Python-list mailing list