I\'m using the new Python Requests library to make http requests. I obtain a cookie from the server as text. How do I turn that into a CookieJar
with the cookie
I'm confused by this question. The requests library will put the cookies in the jar for you.
import requests
import cookielib
URL = '...whatever...'
jar = cookielib.CookieJar()
r = requests.get(URL, cookies=jar)
r = requests.get(URL, cookies=jar)
The first request to the URL will fill the jar. The second request will send the cookies back to the server. The same goes for the standard library's urllib module cookielib. (doc currently available for 2.x Version)
I'm trying to do the same thing. This is what I have so far, and for some reason it isn't sending the cookies along in the header. It might get you far enough along to solve your problem though.
import requests
import cookielib
import logging
log = logging.getLogger(__name__)
def auth(auth_url, cookies):
cj = cookielib.CookieJar()
for x in cookies:
if len(cookies[x]) > 0:
ck = cookielib.Cookie(version=1, name=x, value=cookies[x],
port=None, port_specified=False, domain='.example.com',
domain_specified=True,
domain_initial_dot=True, path='/',
path_specified=True, secure=False,
expires=None, discard=True,
comment=None, comment_url=None,
rest=None, rfc2109=True)
log.info(ck)
cj.set_cookie(ck)
log.info("cookies = %s " % cj)
response = requests.get(auth_url, cookies=cj)
log.info("response %s \n" % response)
log.info("response.headers %s \n" % response.headers)
log.info("response.content %s \n" % response.content)
Simplified version of overthink's answer, on how to get a cookiejar and persist the cookies in Python3:
import requests
s = requests.Session()
r1 = s.get('https://stackoverflow.com')
print("r1",r1.cookies) #Have cookie
print("s",s.cookies) #Have cookie(jar)
r2 = s.get('https://stackoverflow.com') #The cookie from r1 is resend
print("r2",r2.cookies) #No cookie (could be a new one)
print("s",s.cookies) #Keep the cookie(jar) from r1
To persist the cookies between sessions you have to save and reuse the cookiejar in Session (the s variable). If you get different answers between r1/r2/s on other sites, check if there is a redirect. As example will r1/r2 get no cookie for https://www.stackoverflow.com because it is redirected to the site without www.
I think many of these answers are missing the point. Sometimes that other library isn't using requests under the hood. Or doesn't expose the cookiejar it's using. Sometimes all we have is the cookie string. In my case I'm trying to borrow the auth cookie from pyVmomi.
import requests
import http.cookies
raw_cookie_line = 'foo="a secret value"; Path=/; HttpOnly; Secure; '
simple_cookie = http.cookies.SimpleCookie(raw_cookie_line)
cookie_jar = requests.cookies.RequestsCookieJar()
cookie_jar.update(simple_cookie)
Which gives us the following cookie_jar
:
In [5]: cookie_jar
Out[5]: <RequestsCookieJar[Cookie(version=0, name='foo', value='a secret value', port=None, port_specified=False, domain='', domain_specified=False, domain_initial_dot=False, path='/', path_specified=True, secure=True, expires=None, discard=False, comment='', comment_url=False, rest={'HttpOnly': True}, rfc2109=False)]>
Which we can use as normal:
requests.get(..., cookies=cookie_jar)
Try this site: Voidspace article
Over the years I have found voidspace to be extremely useful for doing this kind of stuff. Hope I helped, although I'm quite a dunce. The code is available at Voidspace Recipes as source code .py although the download file is a ".py-" file.
Assuming that you have requested for url
and you got headers
as response. Type type of url
is string. Type type of headers
is list.
import urllib2
import cookielib
class dummyResponse:
def __init__(self,headers):
self.headers=headers
def info(self):
return dummyInfo(self.headers)
class dummyInfo:
def __init__(self,headers):
self.headers=headers
def getheaders(self,key):
#Headers are in the form: 'Set-Cookie: key=val\r\n'. We want 'key=val'
newMatches=[]
for header in self.headers:
if header.lower().startswith(key.lower()):
clearHeader=header[len(key)+1:].strip()
newMatches.append(clearHeader)
return newMatches
req=urllib2.Request(url)
resp=dummyResponse(headers)
jar=cookielib.CookieJar()
jar.extract_cookies(resp, req)