[英]Python: Send cookie with urllib2
I need is to send the value of a cookie to a web eg google.com . 我需要将Cookie的值发送到网络,例如google.com 。
I've tried cookielib, but by asking "headers" cookie does not leave me . 我已经尝试过cookielib,但是通过询问“标题” cookie并不会离开我。 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. 据我所知,唯一的方法是构建一个自定义URL打开器,并将HTTPCookieProcessor包含在其处理程序列表中。
It is possible to override the default handler or to simply define your own urlopen() that uses the cookie jar 可以覆盖默认处理程序,也可以简单地定义您自己的使用cookie罐的urlopen()
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.