本文整理汇总了Python中cookielib.Cookie方法的典型用法代码示例。如果您正苦于以下问题:Python cookielib.Cookie方法的具体用法?Python cookielib.Cookie怎么用?Python cookielib.Cookie使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cookielib
的用法示例。
在下文中一共展示了cookielib.Cookie方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: addcookies
# 需要导入模块: import cookielib [as 别名]
# 或者: from cookielib import Cookie [as 别名]
def addcookies(self, cookies):
if not isinstance(cookies, requests.cookies.RequestsCookieJar):
return False
for domain, pathdict in cookies._cookies.items():
dotdomain = domain if domain[0] == '.' else '.' + domain
if dotdomain not in self.cookiedict.keys():
self.cookiedict[dotdomain] = {}
for path, keydict in pathdict.items():
if path not in self.cookiedict[dotdomain].keys():
self.cookiedict[dotdomain][path] = {}
for key, cookieobj in keydict.items():
if isinstance(cookieobj, cookielib.Cookie):
print cookieobj
cookie_attrs = {}
cookie_attrs["value"] = cookieobj.value
cookie_attrs["expires"] = cookieobj.expires
cookie_attrs["secure"] = cookieobj.secure
cookie_attrs["port"] = cookieobj.port
cookie_attrs["version"] = cookieobj.version
self.cookiedict[dotdomain][path][key] = cookie_attrs
示例2: GET_S_VI_COOKIE
# 需要导入模块: import cookielib [as 别名]
# 或者: from cookielib import Cookie [as 别名]
def GET_S_VI_COOKIE(self, referer):
cj = cookielib.LWPCookieJar(os.path.join(ADDON_PATH_PROFILE, 'cookies.lwp'))
cj.load(os.path.join(ADDON_PATH_PROFILE, 'cookies.lwp'),ignore_discard=True)
ck = cookielib.Cookie(version=0, name='s_cc', value='true', port=None, port_specified=False, domain='.comcast.net', domain_specified=False, domain_initial_dot=False, path='/', path_specified=True, secure=False, expires=None, discard=True, comment=None, comment_url=None, rest={'HttpOnly': None}, rfc2109=False)
cj.set_cookie(ck)
ck = cookielib.Cookie(version=0, name='s_sq', value='%5B%5BB%5D%5D', port=None, port_specified=False, domain='.comcast.net', domain_specified=False, domain_initial_dot=False, path='/', path_specified=True, secure=False, expires=None, discard=True, comment=None, comment_url=None, rest={'HttpOnly': None}, rfc2109=False)
cj.set_cookie(ck)
url = "https://serviceos.comcast.net/b/ss/comcastnet/1/H.27.5/s41303345554477?AQB=1&pccr=true&vidn=2C0299A205011584-60000137E007532F&&ndh=1&t=17%2F9%2F2016%2016%3A23%3A32%201%20240&fid=1F8FC523BD7D4396-2E7FF98D1A765CCC&ce=UTF-8&ns=comcast&pageName=mobile%20app%20sign%20in&g=https%3A%2F%2Flogin.comcast.net%2Flogin%3Fr%3Dcomcast.net%26s%3Doauth%26continue%3Dhttps%253A%252F%252Flogin.comcast.net%252Foauth%252Fauthorize%253Fresponse_type%253Dcode%2526redirect_uri%253Dhttps%253A%252F%252Fsp.auth.adobe.com%252Fadobe-services%252Foauth2%2526state%253DQyqYoV%2526scope%253Dopenid%252520prof&cc=USD&ch=sign%20in&events=event11&c1=%2Flogin%2F%3Amobile%20app%20sign%20in&v1=%2Flogin%2F%3Amobile%20app%20sign%20in&c4=sign%20in&c7=adobepass-nbcsports&v7=adobepass-nbcsports&c23=small&c31=comcast&v31=mobile%20app%20sign%20in&c32=cim&v32=cim&c33=comcast%20net&v33=comcast%20net&c34=comcast%20net%3Asign%20in&c35=authentication&v35=authentication&c36=site%3Ahome&v36=site%3Ahome&v41=small&c44=anonymous%3Amobile%20app%20sign%20in&v47=anonymous&h1=comcast%3Acim%3Acomcast%20net%3Asign%20in%3Amobile%20app%20sign%20in&h2=%2Flogin&s=375x667&c=32&j=1.6&v=N&k=Y&bw=375&bh=667&-g=ile%252520https%253A%252F%252Flogin.comcast.net%252Fpdp%252Ftve%2526client_id%253Dadobepass-nbcsports%2526acr_values%253Durn%253Aoasis%253Anames%253Atc%253ASAML%253A2.0%253Aac%253Aclasses%253AInternetProtocol%2526response%253D1%26reqId%3D51e0dfcb-71e4-4a3f-96ad-8b8366155e6b%26ipAddrAuthn%3D1%26client_id%3Dadobepass-nbcsports&AQE=1"
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
opener.addheaders = [ ("Accept", "*/*"),
("Accept-Encoding", "gzip, deflate"),
("Accept-Language", "en-us"),
("Connection", "keep-alive"),
("Referer", referer),
("User-Agent", UA_IPHONE)]
resp = opener.open(url)
resp.close()
SAVE_COOKIE(cj)
示例3: get_cookie
# 需要导入模块: import cookielib [as 别名]
# 或者: from cookielib import Cookie [as 别名]
def get_cookie(name, value):
return cookielib.Cookie(version=0,
name=name,
value=value,
domain=_204_DOMAIN,
domain_specified=False,
domain_initial_dot=False,
path=_204_PATH,
path_specified=False,
secure=False,
expires=None,
comment=None,
comment_url=None,
rest={'HttpOnly':None},
rfc2109=False,
discard=False,
port=None,
port_specified=False
)
示例4: get_url_headers
# 需要导入模块: import cookielib [as 别名]
# 或者: from cookielib import Cookie [as 别名]
def get_url_headers(url, forced=False):
from . import scrapertools
domain = urlparse.urlparse(url)[1]
sub_dom = scrapertools.find_single_match(domain, '\.(.*?\.\w+)')
if sub_dom and not 'google' in url:
domain = sub_dom
domain_cookies = cj._cookies.get("." + domain, {}).get("/", {})
if "|" in url or not "cf_clearance" in domain_cookies:
if not forced:
return url
headers = dict()
headers["User-Agent"] = default_headers["User-Agent"]
headers["Cookie"] = "; ".join(["%s=%s" % (c.name, c.value) for c in list(domain_cookies.values())])
return url + "|" + "&".join(["%s=%s" % (h, urllib.quote(headers[h])) for h in headers])
示例5: make_cookie
# 需要导入模块: import cookielib [as 别名]
# 或者: from cookielib import Cookie [as 别名]
def make_cookie(name, value):
return cookielib.Cookie(
version=0,
name=name,
value=value,
port=None,
port_specified=False,
domain=V2EX_DOMAIN,
domain_specified=True,
domain_initial_dot=False,
path='/',
path_specified=True,
secure=False,
expires=None,
discard=False,
comment=None,
comment_url=None,
rest=None
)
示例6: cookiejar
# 需要导入模块: import cookielib [as 别名]
# 或者: from cookielib import Cookie [as 别名]
def cookiejar(self, domain):
if not domain:
return None
dotdomain = domain if domain[0] == '.' else '.' + domain
exploded = dotdomain.split(".")
parent_domains = [".%s" % (".".join(exploded[x:])) for x in range(1, len(exploded) - 1)]
matching_domains = [d for d in parent_domains if d in self.cookiedict]
if not matching_domains:
return None
cj = cookielib.CookieJar()
for d in matching_domains:
for path in self.cookiedict[d]:
for cookie_name, cookie_attrs in self.cookiedict[d][path].items():
ck = cookielib.Cookie(version=cookie_attrs["version"],
name=cookie_name,
value=cookie_attrs["value"],
port=None,
port_specified=False,
domain=d,
domain_specified=True,
domain_initial_dot=False,
path=path,
path_specified=True,
secure=cookie_attrs["secure"],
expires=cookie_attrs["expires"],
discard=True,
comment=None,
comment_url=None,
rest={'HttpOnly': None},
rfc2109=False)
if cookie_attrs["port"]:
ck.port = cookie_attrs["port"]
ck.port_specified = True
cj.set_cookie(ck)
return cj
示例7: setCookie
# 需要导入模块: import cookielib [as 别名]
# 或者: from cookielib import Cookie [as 别名]
def setCookie(self, key, val, domain):
ck = cookielib.Cookie(version=0, name=key, value=val, port=None, port_specified=False, domain=domain, domain_specified=False, domain_initial_dot=False, path='/', path_specified=True, secure=False, expires=None, discard=True, comment=None, comment_url=None, rest={'HttpOnly': None}, rfc2109=False)
self.__cookie.set_cookie(ck)
#self.__cookie.clear() clean cookie
# vim : tabstop=2 shiftwidth=2 softtabstop=2 expandtab
示例8: saveCookies
# 需要导入模块: import cookielib [as 别名]
# 或者: from cookielib import Cookie [as 别名]
def saveCookies(self, cookiejar):
MozillaCookieJar = cookielib.MozillaCookieJar()
for c in cookiejar:
args = dict(vars(c).items())
args['rest'] = args['_rest']
del args['_rest']
c = cookielib.Cookie(**args)
MozillaCookieJar.set_cookie(c)
MozillaCookieJar.save('youdaoCookies', ignore_discard=True)
示例9: loadCookies
# 需要导入模块: import cookielib [as 别名]
# 或者: from cookielib import Cookie [as 别名]
def loadCookies(self):
if os.path.exists('youdaoCookies'):
self.window.debug.appendPlainText('625: Cookie exists!')
MozillaCookieJar = cookielib.MozillaCookieJar()
MozillaCookieJar.load('youdaoCookies', ignore_discard=True)
return MozillaCookieJar
else:
return False
示例10: login
# 需要导入模块: import cookielib [as 别名]
# 或者: from cookielib import Cookie [as 别名]
def login(self, username, password):
self.window.debug.appendPlainText('633: Process login!')
password = hashlib.md5(password.encode('utf-8')).hexdigest()
url = "https://logindict.youdao.com/login/acc/login"
payload = "username=" + urllib.quote(username) + "&password=" + password + \
"&savelogin=1&app=web&tp=urstoken&cf=7&fr=1&ru=http%3A%2F%2Fdict.youdao.com%2Fwordbook%2Fwordlist%3Fkeyfrom%3Dnull&product=DICT&type=1&um=true&savelogin=1"
headers = {
'Cache-Control': '"no-cache"',
'Referer': 'http://account.youdao.com/login?service=dict&back_url=http://dict.youdao.com/wordbook/wordlist%3Fkeyfrom%3Dlogin_from_dict2.index',
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36',
'Content-Type': 'application/x-www-form-urlencoded'
}
url = url + '?' + payload
req = urllib2.Request(url, headers=headers)
cookie = cookielib.CookieJar()
self.opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie))
self.req = urllib2.install_opener(self.opener)
urllib2.urlopen(req)
if any(username in c.value for c in cookie):
self.saveCookies(cookie)
self.window.debug.appendPlainText('652: Cookie saved')
return True
else:
self.window.debug.appendPlainText('655: Login failed')
self.error = 1
return False
示例11: test_parse_ns_headers_special_names
# 需要导入模块: import cookielib [as 别名]
# 或者: from cookielib import Cookie [as 别名]
def test_parse_ns_headers_special_names(self):
# names such as 'expires' are not special in first name=value pair
# of Set-Cookie: header
from cookielib import parse_ns_headers
# Cookie with name 'expires'
hdr = 'expires=01 Jan 2040 22:23:32 GMT'
expected = [[("expires", "01 Jan 2040 22:23:32 GMT"), ("version", "0")]]
self.assertEqual(parse_ns_headers([hdr]), expected)
示例12: interact_netscape
# 需要导入模块: import cookielib [as 别名]
# 或者: from cookielib import Cookie [as 别名]
def interact_netscape(cookiejar, url, *set_cookie_hdrs):
return _interact(cookiejar, url, set_cookie_hdrs, "Set-Cookie")
示例13: _interact
# 需要导入模块: import cookielib [as 别名]
# 或者: from cookielib import Cookie [as 别名]
def _interact(cookiejar, url, set_cookie_hdrs, hdr_name):
"""Perform a single request / response cycle, returning Cookie: header."""
from urllib2 import Request
req = Request(url)
cookiejar.add_cookie_header(req)
cookie_hdr = req.get_header("Cookie", "")
headers = []
for hdr in set_cookie_hdrs:
headers.append("%s: %s" % (hdr_name, hdr))
res = FakeResponse(headers, url)
cookiejar.extract_cookies(res, req)
return cookie_hdr
示例14: test_missing_value
# 需要导入模块: import cookielib [as 别名]
# 或者: from cookielib import Cookie [as 别名]
def test_missing_value(self):
from cookielib import MozillaCookieJar, lwp_cookie_str
# missing = sign in Cookie: header is regarded by Mozilla as a missing
# name, and by cookielib as a missing value
filename = test_support.TESTFN
c = MozillaCookieJar(filename)
interact_netscape(c, "http://www.acme.com/", 'eggs')
interact_netscape(c, "http://www.acme.com/", '"spam"; path=/foo/')
cookie = c._cookies["www.acme.com"]["/"]["eggs"]
self.assertIsNone(cookie.value)
self.assertEqual(cookie.name, "eggs")
cookie = c._cookies["www.acme.com"]['/foo/']['"spam"']
self.assertIsNone(cookie.value)
self.assertEqual(cookie.name, '"spam"')
self.assertEqual(lwp_cookie_str(cookie), (
r'"spam"; path="/foo/"; domain="www.acme.com"; '
'path_spec; discard; version=0'))
old_str = repr(c)
c.save(ignore_expires=True, ignore_discard=True)
try:
c = MozillaCookieJar(filename)
c.revert(ignore_expires=True, ignore_discard=True)
finally:
os.unlink(c.filename)
# cookies unchanged apart from lost info re. whether path was specified
self.assertEqual(
repr(c),
re.sub("path_specified=%s" % True, "path_specified=%s" % False,
old_str)
)
self.assertEqual(interact_netscape(c, "http://www.acme.com/foo/"),
'"spam"; eggs')
示例15: test_ns_parser_special_names
# 需要导入模块: import cookielib [as 别名]
# 或者: from cookielib import Cookie [as 别名]
def test_ns_parser_special_names(self):
# names such as 'expires' are not special in first name=value pair
# of Set-Cookie: header
from cookielib import CookieJar
c = CookieJar()
interact_netscape(c, "http://www.acme.com/", 'expires=eggs')
interact_netscape(c, "http://www.acme.com/", 'version=eggs; spam=eggs')
cookies = c._cookies["www.acme.com"]["/"]
self.assertTrue('expires' in cookies)
self.assertTrue('version' in cookies)