當前位置: 首頁>>代碼示例>>Python>>正文


Python cookie.Cookie類代碼示例

本文整理匯總了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'))
開發者ID:guanwei,項目名稱:freeipa,代碼行數:7,代碼來源:test_cookie.py

示例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))
開發者ID:guanwei,項目名稱:freeipa,代碼行數:14,代碼來源:test_cookie.py

示例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
開發者ID:stlaz,項目名稱:freeipa,代碼行數:31,代碼來源:rpc.py

示例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
開發者ID:andygabby,項目名稱:freeipa,代碼行數:31,代碼來源:session.py

示例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
開發者ID:AvidehST,項目名稱:freeipa,代碼行數:35,代碼來源:rpc.py

示例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
開發者ID:stlaz,項目名稱:freeipa,代碼行數:59,代碼來源:rpc.py

示例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')
開發者ID:guanwei,項目名稱:freeipa,代碼行數:8,代碼來源:test_cookie.py

示例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")
開發者ID:apophys,項目名稱:freeipa,代碼行數:8,代碼來源:test_cookie.py

示例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))
開發者ID:apophys,項目名稱:freeipa,代碼行數:13,代碼來源:test_cookie.py

示例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"
開發者ID:apophys,項目名稱:freeipa,代碼行數:18,代碼來源:test_cookie.py

示例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
開發者ID:apophys,項目名稱:freeipa,代碼行數:22,代碼來源:rpc.py

示例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'
開發者ID:guanwei,項目名稱:freeipa,代碼行數:18,代碼來源:test_cookie.py

示例13: test_no_attributes

 def test_no_attributes(self):
     cookie = Cookie('color', 'blue')
     self.assertTrue(cookie.http_return_ok(self.url))
開發者ID:guanwei,項目名稱:freeipa,代碼行數:3,代碼來源:test_cookie.py

示例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.
開發者ID:AvidehST,項目名稱:freeipa,代碼行數:30,代碼來源:rpc.py

示例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"))
開發者ID:apophys,項目名稱:freeipa,代碼行數:21,代碼來源:test_cookie.py


注:本文中的ipapython.cookie.Cookie類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。