I need to get the cookies from a HTTP response sent by a server and put it in the next request's header. How can I do it?
Thanks in advance.
I need to get the cookies from a HTTP response sent by a server and put it in the next request's header. How can I do it?
Thanks in advance.
You can use in Python 2.7
and when sending request back
Current answer is to use Requests module and the requests.Session object.
You may need to
pip install requests
orpipenv install requests
first.You should use the cookielib module with urllib.
It will store cookies between requests, and you can load/save them on disk. Here is an example:
Notice that the values for
NID
andPREF
are the same between requests. If you omitted theHTTPCookieProcessor
these would be different (urllib2 wouldn't sendCookie
headers on the 2nd request).Look at urllib module:
(with Python 3.1, in Python 2, use urllib2.urlopen instead) For retrieving cookies:
And for sending, simply send a Cookie header with request. Like that:
Edit:
The "http.cookie"("Cookie" for Python 2) may work for you better:
http://docs.python.org/library/cookie.html