Questions tagged [cookielib]

The cookielib module defines classes for automatic handling of HTTP cookies. It is useful for accessing web sites that require small pieces of data – cookies – to be set on the client machine by an HTTP response from a web server, and then returned to the server in later HTTP requests.

The cookielib module of Python 2 defines classes for automatic handling of HTTP cookies.

The following classes are provided:

cookielib.CookieJar: stores HTTP cookies. It extracts cookies from HTTP requests and returns them in HTTP responses.

cookielib.FileCookieJar: load and save cookies from a file.

cookielib.DefaultCookiePolicy: implements the standard accept and reject rules for Netscape and RFC 2965 cookies. By default, RFC 2109 cookies are treated according to the RFC 2965 rules.

cookielib.Cookie: represents Netscape, RFC 2109 and RFC 2965 cookies.

The cookielib module has been renamed to http.cookiejar in Python 3.

For more information look at https://docs.python.org/2/library/cookielib.html

65 questions
33
votes
4 answers

Get cookie from CookieJar by name

I know that I can iterate through the cookies in a cookiejar, and this would allow me to find a cookie with a particular name - but does the CookieJar object itself have any methods I can call to get a certain cookie by name? It just saves me having…
Matt
  • 11,157
  • 26
  • 81
  • 110
26
votes
5 answers

Using cookies.txt file with Python Requests

I'm trying to access an authenticated site using a cookies.txt file (generated with a Chrome extension) with Python Requests: import requests, cookielib cj = cookielib.MozillaCookieJar('cookies.txt') cj.load() r = requests.get(url, cookies=cj) It…
cjauvin
  • 3,433
  • 4
  • 29
  • 38
22
votes
3 answers

Python - urllib2 & cookielib

I am trying to open the following website and retrieve the initial cookie and use it for the second url-open BUT if you run the following code it outputs 2 different cookies. How do I use the initial cookie for the second url-open? import cookielib,…
Adrian
  • 275
  • 1
  • 3
  • 5
14
votes
1 answer

How can I add a cookie to an existing cookielib CookieJar instance in Python?

I have a CookieJar that's being used with Mechanize that I want to add a cookie to. How can I go about doing this? make_cookie() and set_cookie() weren't clear enough for me. br = mechanize.Browser() cj =…
Paul
  • 519
  • 1
  • 4
  • 6
9
votes
2 answers

How to pickle a CookieJar?

I have an object with a CookieJar that I want to pickle. However as you all probably know, pickle chokes on objects that contain lock objects. And for some horrible reason, a CookieJar has a lock object. from cPickle import dumps from cookielib…
Unknown
  • 45,913
  • 27
  • 138
  • 182
7
votes
1 answer

loading cookies from selenium to mechanize with cookielib

I am trying to login to a website with selenium, then transfer the cookie to mechanize. I have successfully logged in with selenium and saved its session cookie to a variable. The problem comes when trying to load the cookie with cookielib. Relevant…
user3053161
  • 291
  • 3
  • 8
5
votes
1 answer

Python how to preserve HTTP cookies

I used this piece to 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. //…
CuriousMind
  • 15,168
  • 20
  • 82
  • 120
4
votes
1 answer

AttributeError in Python when creating FileCookieJar (cookielib Py 2.6)

I'm trying to create a new FileCookieJar using cookielib in Python 2.6. However, when I do so I get the following error: Traceback (most recent call last): File "C:\Users\Paul\getfile.py", line 7, in…
codinghands
  • 1,741
  • 2
  • 18
  • 31
4
votes
2 answers

Storing cookielib cookies in a database

I'm using the cookielib module to handle HTTP cookies when using the urllib2 module in Python 2.6 in a way similar to this snippet: import cookielib, urllib2 cj = cookielib.CookieJar() opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj)) r…
Mridang Agarwalla
  • 43,201
  • 71
  • 221
  • 382
3
votes
2 answers

Fetching cookies from a file with python

I'm using mechanize and python to log into a site. I've created two functions. The first one logs in and the second one searches the site. How exactly do I store the cookies from the login so when I come to searching I have a cookie. Current…
Michael Esteves
  • 1,445
  • 3
  • 21
  • 38
3
votes
1 answer

How to add a cookie to existing cookies in MechanicalSoup

I know MechanicalSoup has a function called set_cookiejar() but it replaces the current cookiejar completely. I want to know how to add new cookies to existing cookies.
3
votes
1 answer

How to display specific cookie?

I'm using ActivePython 2.5.1 and the cookielib package to retrieve web pages. I'd like to display a given cookie from the cookiejar instead of the whole thing: #OK to display all the cookies for index, cookie in enumerate(cj): print index, ' : …
Gulbahar
  • 5,343
  • 20
  • 70
  • 93
3
votes
1 answer

How do I save a Netscape style cookies.txt file using Python 2.7 and Requests?

I am using requests to fetch a webpage and I would like to save the cookies in a Netscape-style cookies.txt file. How can I achieve this? I have tried the following: import requests import cookielib url = 'http://www.yahoo.com' ua = ("Mozilla/5.0…
Holy Mackerel
  • 3,259
  • 1
  • 25
  • 41
2
votes
2 answers

Cookiejar use in an opener

I have the following code at the moment: tw_jar = cookielib.CookieJar() tw_jar.set_cookie(c1) tw_jar.set_cookie(c2) o = urllib2.build_opener( urllib2.HTTPCookieProcessor(tw_jar) ) urllib2.install_opener( o ) Now I later in my code I don't want to…
user774166
  • 45
  • 3
2
votes
0 answers

check whether a session cookie is still alive (Python)

Please take a look at the following python code snippet : import cookielib, urllib, urllib2 def login(username, password): cookie_jar = cookielib.LWPCookieJar() opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie_jar)) …
dexter
  • 21
  • 1
  • 3
1
2 3 4 5