I have exported cookies from Chrome using cookies.txt Chrome plugin. I can easily replay the request using curl with the cookies exported. Some code like curl -b mycookie.txt -c mycookie.txt $url works perfectly.
However, when I was trying to load the session from mycookie.txt using requests, it can't work. After long time debugging, I fond out that requests will not send session cookies(whose expire values is 0 in cookies file) even though I already loaded the expired cookie with following code:
cj = cookielib.MozillaCookieJar('mycookie.txt')
cj.load(ignore_discard=True, ignore_expires=True)
print cj #it shows the session cookie already loaded
s = requests.Session()
s.cookies = cj
r = s.get(url, headers=myheaders, timeout=10, allow_redirects=False)
print r.request.headers #this clearly shows the request didn't send the session cookie
How can I make it work?
It happened to me. Let's take a closer look at
requests/models.py:Goto
requests/cookies.py:Goto
http/cookiejar.py:It means that
localhostwithout a dot will be concatenated tolocalhost.local. If, for instance, my cookie file is edited with thelocalhostdomain, it will not send the cookie because the domain does not match (curlhas no such issue):The cookie will be sent after it appends
.local(Note thathttp's localhost only selects lines with the fourth column equal toFALSE):Alternatively, I can use the same IP, e.g.,
s.get(http://127.0.0.1:<port>/...)and127.0.0.1 FALSE / FALSE 1712121230 token xxxxx