How to set and retrieve cookie in HTTP header in Python?

Look at urllib module:

(with Python 3.1, in Python 2, use urllib2.urlopen instead) For retrieving cookies:

>>> import urllib.request
>>> d = urllib.request.urlopen("http://www.google.co.uk")
>>> d.getheader('Set-Cookie')
'PREF=ID=a45c444aa509cd98:FF=0:TM=14.....'

And for sending, simply send a Cookie header with request. Like that:

r=urllib.request.Request("http://www.example.com/",headers={'Cookie':"session_id=1231245546"})
urllib.request.urlopen(r)

Edit:

The "http.cookie"("Cookie" for Python 2) may work for you better:

http://docs.python.org/library/cookie.html


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:

import cookielib
import urllib2

cookies = cookielib.LWPCookieJar()
handlers = [
    urllib2.HTTPHandler(),
    urllib2.HTTPSHandler(),
    urllib2.HTTPCookieProcessor(cookies)
    ]
opener = urllib2.build_opener(*handlers)

def fetch(uri):
    req = urllib2.Request(uri)
    return opener.open(req)

def dump():
    for cookie in cookies:
        print cookie.name, cookie.value

uri = 'http://www.google.com/'
res = fetch(uri)
dump()

res = fetch(uri)
dump()

# save cookies to disk. you can load them with cookies.load() as well.
cookies.save('mycookies.txt')

Notice that the values for NID and PREF are the same between requests. If you omitted the HTTPCookieProcessor these would be different (urllib2 wouldn't send Cookie headers on the 2nd request).


You can use in Python 2.7

url="http://google.com"
request = urllib2.Request(url)
sock=urllib2.urlopen(request)
cookies=sock.info()['Set-Cookie']
content=sock.read()
sock.close()
print (cookies, content)

and when sending request back

def sendResponse(cookies): 
import urllib
request = urllib2.Request("http://google.com")
request.add_header("Cookie", cookies)
request.add_data(urllib.urlencode([('arg1','val1'),('arg1','val1')]))
opener=urllib2
opener=urllib2.build_opener(urllib2.HTTPHandler(debuglevel=1))
sock=opener.open(request)
content=sock.read()
sock.close()
print len(content)