I need is to send the value of a cookie to a web eg google.com.
I've tried cookielib, but by asking "headers" cookie does not leave me . How can I send?
I need is to send the value of a cookie to a web eg google.com.
I've tried cookielib, but by asking "headers" cookie does not leave me . How can I send?
As far is I know, the only way to do this is to build a custom URL opener and include HTTPCookieProcessor in its list of handlers.
It is possible to override the default handler or to simply define your own urlopen() that uses the cookie jar
cookiejar = cookielib.CookieJar()
cookie_handler = urllib2.HTTPCookieProcessor(cookiejar)
# build_opener() automatically adds default handlers up front
opener = urllib2.build_opener(cookie_handler)
# override global opener
urllib2.install_opener(opener)
# alternative version that does not override the global opener
def myurlopen(url, data=None):
req = urllib2.Request(url)
return opener.open(req, data)
# add a custom cookie
cookie = cookielib.Cookie(
None, # version
'Header', 'value', # name, value
port, False, # port, port_specified
'example.org', True, False # domain, domain_specified, domain_initial_dot
'/', True, # path, path_specified
False, # secure (needs https)
None, # expires
False, # discard
None, None, # comment, comment_url
False # rest
)
cookiejar.set_cookie(cookie)