本文整理汇总了Python中http.cookiejar.Cookie方法的典型用法代码示例。如果您正苦于以下问题:Python cookiejar.Cookie方法的具体用法?Python cookiejar.Cookie怎么用?Python cookiejar.Cookie使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类http.cookiejar
的用法示例。
在下文中一共展示了cookiejar.Cookie方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_cookie_dict
# 需要导入模块: from http import cookiejar [as 别名]
# 或者: from http.cookiejar import Cookie [as 别名]
def test_cookie_dict():
c = RetsHttpClient('login_url', 'username', 'password')
c._session = mock.MagicMock()
jar = RequestsCookieJar()
c1 = Cookie(1, 'name1', 'value1', 80, 80, 'domain', 'domain_specified', 'domain_initial_dot', 'path',
'path_specified', True, True, False, 'comment', 'comment_url', 'rest')
c2 = Cookie(1, 'name2', 'value2', 80, 80, 'domain', 'domain_specified', 'domain_initial_dot', 'path',
'path_specified', True, True, False, 'comment', 'comment_url', 'rest')
c3 = Cookie(1, 'name1', 'value1', 80, 80, 'domain', 'domain_specified3', 'domain_initial_dot3', 'path3',
'path_specified3', True, True, False, 'comment', 'comment_url', 'rest')
jar.set_cookie(c1)
jar.set_cookie(c2)
jar.set_cookie(c3)
c._session.cookies = jar
assert c.cookie_dict == {'name1': 'value1', 'name2': 'value2'}
示例2: test_request_loader_does_not_fail_with_invalid_token
# 需要导入模块: from http import cookiejar [as 别名]
# 或者: from http.cookiejar import Cookie [as 别名]
def test_request_loader_does_not_fail_with_invalid_token(client):
c = Cookie(
version=0,
name="remember_token",
value="None",
port=None,
port_specified=False,
domain="www.example.com",
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,
)
client.cookie_jar.set_cookie(c)
response = client.get("/")
assert b"BadSignature" not in response.data
示例3: make_cookie
# 需要导入模块: from http import cookiejar [as 别名]
# 或者: from http.cookiejar import Cookie [as 别名]
def make_cookie(self, name, value):
return Cookie(
version=0,
name=name,
value=value,
port=None,
port_specified=False,
domain="music.163.com",
domain_specified=True,
domain_initial_dot=False,
path="/",
path_specified=True,
secure=False,
expires=None,
discard=False,
comment=None,
comment_url=None,
rest={},
)
示例4: get_url_headers
# 需要导入模块: from http import cookiejar [as 别名]
# 或者: from http.cookiejar 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: _getCookies
# 需要导入模块: from http import cookiejar [as 别名]
# 或者: from http.cookiejar import Cookie [as 别名]
def _getCookies(headers):
cj = CookieJar()
cookies = headers.split(',')
for cookie in cookies:
attrs = cookie.split(';')
cookieNameValue = name = attrs[0].strip().split('=')
pathNameValue = name = attrs[1].strip().split('=')
ck = Cookie(version=1, name=cookieNameValue[0],
value=cookieNameValue[1],
port=443,
port_specified=False,
domain=swaDomain,
domain_specified=True,
domain_initial_dot=False,
path=pathNameValue[1],
path_specified=True,
secure=True,
expires=None,
discard=True,
comment=None,
comment_url=None,
rest={'HttpOnly': None},
rfc2109=False)
cj.set_cookie(ck)
return cj
示例6: save_cookiestxt
# 需要导入模块: from http import cookiejar [as 别名]
# 或者: from http.cookiejar import Cookie [as 别名]
def save_cookiestxt(fp, cookies):
"""Write 'cookies' in Netscape cookies.txt format to 'fp'"""
fp.write("# Netscape HTTP Cookie File\n\n")
for cookie in cookies:
if cookie.value is None:
name = ""
value = cookie.name
else:
name = cookie.name
value = cookie.value
fp.write("\t".join((
cookie.domain,
"TRUE" if cookie.domain.startswith(".") else "FALSE",
cookie.path,
"TRUE" if cookie.secure else "FALSE",
"0" if cookie.expires is None else str(cookie.expires),
name,
value,
)) + "\n")
示例7: test_anticsrf_without_samesite
# 需要导入模块: from http import cookiejar [as 别名]
# 或者: from http.cookiejar import Cookie [as 别名]
def test_anticsrf_without_samesite(self):
cookie = Cookie(name='CSRFTOKEN',
comment=None,
comment_url=None,
discard=False,
domain='mozilla.com',
domain_initial_dot=False,
domain_specified='mozilla.com',
expires=None,
path='/',
path_specified='/',
port=443,
port_specified=443,
rfc2109=False,
rest={'HttpOnly': True},
secure=True,
version=1,
value='bar')
self.reqs['session'].cookies.set_cookie(cookie)
result = cookies(self.reqs)
self.assertEquals('cookies-anticsrf-without-samesite-flag', result['result'])
self.assertFalse(result['pass'])
self.assertFalse(result['sameSite'])
示例8: test_samesite_invalid
# 需要导入模块: from http import cookiejar [as 别名]
# 或者: from http.cookiejar import Cookie [as 别名]
def test_samesite_invalid(self):
cookie = Cookie(name='SESSIONID',
comment=None,
comment_url=None,
discard=False,
domain='mozilla.com',
domain_initial_dot=False,
domain_specified='mozilla.com',
expires=None,
path='/',
path_specified='/',
port=443,
port_specified=443,
rfc2109=False,
rest={'HttpOnly': True, 'SameSite': 'Invalid'},
secure=True,
version=1,
value='bar')
self.reqs['session'].cookies.set_cookie(cookie)
result = cookies(self.reqs)
self.assertEquals('cookies-samesite-flag-invalid', result['result'])
self.assertFalse(result['pass'])
self.assertIsNone(result['sameSite'])
示例9: test_no_secure
# 需要导入模块: from http import cookiejar [as 别名]
# 或者: from http.cookiejar import Cookie [as 别名]
def test_no_secure(self):
cookie = Cookie(name='foo',
comment=None,
comment_url=None,
discard=False,
domain='mozilla.com',
domain_initial_dot=False,
domain_specified='mozilla.com',
expires=None,
path='/',
path_specified='/',
port=443,
port_specified=443,
rfc2109=False,
rest={},
secure=False,
version=1,
value='bar')
self.reqs['session'].cookies.set_cookie(cookie)
result = cookies(self.reqs)
self.assertEquals('cookies-without-secure-flag', result['result'])
self.assertFalse(result['pass'])
self.assertFalse(result['sameSite'])
示例10: init_session_cookie
# 需要导入模块: from http import cookiejar [as 别名]
# 或者: from http.cookiejar import Cookie [as 别名]
def init_session_cookie(name, value, **kwargs):
return Cookie(version=0, name=name, value=value, port=None, port_specified=False,
domain='www.codechef.com', domain_specified=False, domain_initial_dot=False,
path='/', path_specified=True, secure=False, expires=None, discard=False,
comment=None, comment_url=None, rest={'HttpOnly': None}, rfc2109=False)
示例11: test_init_session_cookie
# 需要导入模块: from http import cookiejar [as 别名]
# 或者: from http.cookiejar import Cookie [as 别名]
def test_init_session_cookie(self):
"""Should return cookiejar.Cookie instance with name and value as provided"""
cookie = init_session_cookie("u", "u")
self.assertIsInstance(cookie, Cookie)
self.assertEqual(cookie.name, "u")
self.assertEqual(cookie.value, "u")
示例12: session_get
# 需要导入模块: from http import cookiejar [as 别名]
# 或者: from http.cookiejar import Cookie [as 别名]
def session_get(self):
if self.session:
return self.session
else:
cookie = self.headers.get('Cookie')
if cookie:
cookie = SimpleCookie(cookie)
if self.COOKIE_NAME in cookie:
return cookie[self.COOKIE_NAME].value
return None
示例13: cookiejar_create
# 需要导入模块: from http import cookiejar [as 别名]
# 或者: from http.cookiejar import Cookie [as 别名]
def cookiejar_create(self, cookiejar_file, session):
cookie_jar = LWPCookieJar(cookiejar_file.name)
cookie_jar.set_cookie(Cookie(0, self.COOKIE_NAME, session,
None, False,
'', False, True,
'/', True,
True,
None, None, None, None, {}))
cookie_jar.save()
cookiejar_file.flush()
示例14: addcookies
# 需要导入模块: from http import cookiejar [as 别名]
# 或者: from http.cookiejar import Cookie [as 别名]
def addcookies(self, cookies):
"""Inject Cookies from a CookieJar into our JSON dictionary."""
if not isinstance(cookies, RequestsCookieJar):
return False
for domain, pathdict in cookies._cookies.items():
search_ip = IP_REGEX.match(domain)
if search_ip:
# Match either an IPv4 address or an IPv6 address with a local suffix
domain_key = search_ip.group("ip")
else:
domain_key = domain if domain[0] == '.' else '.' + domain
if domain_key not in self.cookiedict.keys():
self.cookiedict[domain_key] = {}
for path, keydict in pathdict.items():
if path not in self.cookiedict[domain_key].keys():
self.cookiedict[domain_key][path] = {}
for key, cookieobj in keydict.items():
if isinstance(cookieobj, Cookie):
print(cookieobj)
cookie_attrs = {
"value": cookieobj.value,
"expires": cookieobj.expires,
"secure": cookieobj.secure,
"port": cookieobj.port,
"version": cookieobj.version
}
self.cookiedict[domain_key][path][key] = cookie_attrs
示例15: create_cookie
# 需要导入模块: from http import cookiejar [as 别名]
# 或者: from http.cookiejar import Cookie [as 别名]
def create_cookie(host, path, secure, expires, name, value):
"""Shortcut function to create a cookie
"""
return cookielib.Cookie(0, name, value, None, False, host, host.startswith('.'),
host.startswith('.'), path, True, secure, expires, False,
None, None, {})