本文整理汇总了Python中ipapython.cookie.Cookie类的典型用法代码示例。如果您正苦于以下问题:Python Cookie类的具体用法?Python Cookie怎么用?Python Cookie使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Cookie类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_httponly
def test_httponly(self):
cookie = Cookie('color', 'blue', httponly=True)
self.assertTrue(cookie.http_return_ok('http://example.com'))
self.assertTrue(cookie.http_return_ok('https://example.com'))
with self.assertRaises(Cookie.URLMismatch):
self.assertTrue(cookie.http_return_ok('ftp://example.com'))
示例2: test_expires
def test_expires(self):
now = datetime.datetime.utcnow().replace(microsecond=0)
# expires 1 day from now
expires = now + datetime.timedelta(days=1)
cookie = Cookie('color', 'blue', expires=expires)
self.assertTrue(cookie.http_return_ok(self.url))
# expired 1 day ago
expires = now + datetime.timedelta(days=-1)
cookie = Cookie('color', 'blue', expires=expires)
with self.assertRaises(Cookie.Expired):
self.assertTrue(cookie.http_return_ok(self.url))
示例3: get_session_cookie_from_persistent_storage
def get_session_cookie_from_persistent_storage(self, principal):
'''
Retrieves the session cookie for the given principal from the
persistent secure storage. Returns None if not found or unable
to retrieve the session cookie for any reason, otherwise
returns a Cookie object containing the session cookie.
'''
# Get the session data, it should contain a cookie string
# (possibly with more than one cookie).
try:
cookie_string = read_persistent_client_session_data(principal)
if cookie_string is None:
return None
cookie_string = cookie_string.decode('utf-8')
except Exception as e:
logger.debug('Error reading client session data: %s', e)
return None
# Search for the session cookie within the cookie string
try:
session_cookie = Cookie.get_named_cookie_from_string(
cookie_string, COOKIE_NAME,
timestamp=datetime.datetime.utcnow())
except Exception as e:
logger.debug(
'Error retrieving cookie from the persistent storage: %s',
e)
return None
return session_cookie
示例4: get_session_id_from_http_cookie
def get_session_id_from_http_cookie(self, cookie_header):
'''
Parse an HTTP cookie header and search for our session
id. Return the session id if found, return None if not
found.
:parameters:
cookie_header
An HTTP cookie header. May be None, if None return None.
:returns:
Session id as string or None if not found.
'''
if cookie_header is None:
return None
session_id = None
try:
session_cookie = Cookie.get_named_cookie_from_string(cookie_header, self.session_cookie_name)
except Exception as e:
session_cookie = None
if session_cookie:
session_id = session_cookie.value
if session_id is None:
self.debug('no session cookie found')
else:
self.debug('found session cookie_id = %s', session_id)
return session_id
示例5: store_session_cookie
def store_session_cookie(self, cookie_header):
'''
Given the contents of a Set-Cookie header scan the header and
extract each cookie contained within until the session cookie
is located. Examine the session cookie if the domain and path
are specified, if not update the cookie with those values from
the request URL. Then write the session cookie into the key
store for the principal. If the cookie header is None or the
session cookie is not present in the header no action is
taken.
Context Dependencies:
The per thread context is expected to contain:
principal
The current pricipal the HTTP request was issued for.
request_url
The URL of the HTTP request.
'''
if cookie_header is None:
return
principal = getattr(context, 'principal', None)
request_url = getattr(context, 'request_url', None)
root_logger.debug("received Set-Cookie '%s'", cookie_header)
# Search for the session cookie
try:
session_cookie = Cookie.get_named_cookie_from_string(cookie_header,
COOKIE_NAME, request_url)
except Exception, e:
root_logger.error("unable to parse cookie header '%s': %s", cookie_header, e)
return
示例6: store_session_cookie
def store_session_cookie(self, cookie_header):
'''
Given the contents of a Set-Cookie header scan the header and
extract each cookie contained within until the session cookie
is located. Examine the session cookie if the domain and path
are specified, if not update the cookie with those values from
the request URL. Then write the session cookie into the key
store for the principal. If the cookie header is None or the
session cookie is not present in the header no action is
taken.
Context Dependencies:
The per thread context is expected to contain:
principal
The current pricipal the HTTP request was issued for.
request_url
The URL of the HTTP request.
'''
if cookie_header is None:
return
principal = getattr(context, 'principal', None)
request_url = getattr(context, 'request_url', None)
logger.debug("received Set-Cookie (%s)'%s'", type(cookie_header),
cookie_header)
if not isinstance(cookie_header, list):
cookie_header = [cookie_header]
# Search for the session cookie
session_cookie = None
try:
for cookie in cookie_header:
session_cookie = (
Cookie.get_named_cookie_from_string(
cookie, COOKIE_NAME, request_url,
timestamp=datetime.datetime.utcnow())
)
if session_cookie is not None:
break
except Exception as e:
logger.error("unable to parse cookie header '%s': %s",
cookie_header, e)
return
if session_cookie is None:
return
cookie_string = self._slice_session_cookie(session_cookie)
logger.debug("storing cookie '%s' for principal %s",
cookie_string, principal)
try:
update_persistent_client_session_data(principal, cookie_string)
except Exception as e:
# Not fatal, we just can't use the session cookie we were sent.
pass
示例7: test_path_normalization
def test_path_normalization(self):
self.assertEqual(Cookie.normalize_url_path(''), '/')
self.assertEqual(Cookie.normalize_url_path('foo'), '/')
self.assertEqual(Cookie.normalize_url_path('foo/'), '/')
self.assertEqual(Cookie.normalize_url_path('/foo'), '/')
self.assertEqual(Cookie.normalize_url_path('/foo/'), '/foo')
self.assertEqual(Cookie.normalize_url_path('/Foo/bar'), '/foo')
self.assertEqual(Cookie.normalize_url_path('/foo/baR/'), '/foo/bar')
示例8: test_path_normalization
def test_path_normalization(self):
self.assertEqual(Cookie.normalize_url_path(""), "/")
self.assertEqual(Cookie.normalize_url_path("foo"), "/")
self.assertEqual(Cookie.normalize_url_path("foo/"), "/")
self.assertEqual(Cookie.normalize_url_path("/foo"), "/")
self.assertEqual(Cookie.normalize_url_path("/foo/"), "/foo")
self.assertEqual(Cookie.normalize_url_path("/Foo/bar"), "/foo")
self.assertEqual(Cookie.normalize_url_path("/foo/baR/"), "/foo/bar")
示例9: test_path
def test_path(self):
cookie = Cookie("color", "blue")
self.assertTrue(cookie.http_return_ok(self.url))
cookie = Cookie("color", "blue", path="/")
self.assertTrue(cookie.http_return_ok(self.url))
cookie = Cookie("color", "blue", path="/one")
self.assertTrue(cookie.http_return_ok(self.url))
cookie = Cookie("color", "blue", path="/oneX")
with self.assertRaises(Cookie.URLMismatch):
self.assertTrue(cookie.http_return_ok(self.url))
示例10: test_invalid
def test_invalid(self):
# Invalid Max-Age
s = "color=blue; Max-Age=over-the-hill"
with self.assertRaises(ValueError):
Cookie.parse(s)
cookie = Cookie("color", "blue")
with self.assertRaises(ValueError):
cookie.max_age = "over-the-hill"
# Invalid Expires
s = "color=blue; Expires=Sun, 06 Xxx 1994 08:49:37 GMT"
with self.assertRaises(ValueError):
Cookie.parse(s)
cookie = Cookie("color", "blue")
with self.assertRaises(ValueError):
cookie.expires = "Sun, 06 Xxx 1994 08:49:37 GMT"
示例11: get_session_cookie_from_persistent_storage
def get_session_cookie_from_persistent_storage(self, principal):
"""
Retrieves the session cookie for the given principal from the
persistent secure storage. Returns None if not found or unable
to retrieve the session cookie for any reason, otherwise
returns a Cookie object containing the session cookie.
"""
# Get the session data, it should contain a cookie string
# (possibly with more than one cookie).
try:
cookie_string = read_persistent_client_session_data(principal)
except Exception:
return None
# Search for the session cookie within the cookie string
try:
session_cookie = Cookie.get_named_cookie_from_string(cookie_string, COOKIE_NAME)
except Exception:
return None
return session_cookie
示例12: test_invalid
def test_invalid(self):
# Invalid Max-Age
s = 'color=blue; Max-Age=over-the-hill'
with self.assertRaises(ValueError):
cookies = Cookie.parse(s)
cookie = Cookie('color', 'blue')
with self.assertRaises(ValueError):
cookie.max_age = 'over-the-hill'
# Invalid Expires
s = 'color=blue; Expires=Sun, 06 Xxx 1994 08:49:37 GMT'
with self.assertRaises(ValueError):
cookies = Cookie.parse(s)
cookie = Cookie('color', 'blue')
with self.assertRaises(ValueError):
cookie.expires = 'Sun, 06 Xxx 1994 08:49:37 GMT'
示例13: test_no_attributes
def test_no_attributes(self):
cookie = Cookie('color', 'blue')
self.assertTrue(cookie.http_return_ok(self.url))
示例14: read_persistent_client_session_data
Retrieves the session cookie for the given principal from the
persistent secure storage. Returns None if not found or unable
to retrieve the session cookie for any reason, otherwise
returns a Cookie object containing the session cookie.
'''
# Get the session data, it should contain a cookie string
# (possibly with more than one cookie).
try:
cookie_string = read_persistent_client_session_data(principal)
except Exception, e:
return None
# Search for the session cookie within the cookie string
try:
session_cookie = Cookie.get_named_cookie_from_string(cookie_string, COOKIE_NAME)
except Exception, e:
return None
return session_cookie
def apply_session_cookie(self, url):
'''
Attempt to load a session cookie for the current principal
from the persistent secure storage. If the cookie is
successfully loaded adjust the input url's to point to the
session path and insert the session cookie into the per thread
context for later insertion into the HTTP request. If the
cookie is not successfully loaded then the original url is
returned and the per thread context is not modified.
示例15: test_domain
def test_domain(self):
cookie = Cookie("color", "blue", domain="www.foo.bar.com")
self.assertTrue(cookie.http_return_ok(self.url))
cookie = Cookie("color", "blue", domain=".foo.bar.com")
self.assertTrue(cookie.http_return_ok(self.url))
cookie = Cookie("color", "blue", domain=".bar.com")
self.assertTrue(cookie.http_return_ok(self.url))
cookie = Cookie("color", "blue", domain="bar.com")
with self.assertRaises(Cookie.URLMismatch):
self.assertTrue(cookie.http_return_ok(self.url))
cookie = Cookie("color", "blue", domain="bogus.com")
with self.assertRaises(Cookie.URLMismatch):
self.assertTrue(cookie.http_return_ok(self.url))
cookie = Cookie("color", "blue", domain="www.foo.bar.com")
with self.assertRaises(Cookie.URLMismatch):
self.assertTrue(cookie.http_return_ok("http://192.168.1.1/one/two"))