我们从Python开源项目中,提取了以下7个代码示例,用于说明如何使用cookielib.domain_match()。
def get_auth_header(self, host): auth = None for domain, creds in self.gitcookies.iteritems(): if cookielib.domain_match(host, domain): auth = (creds[0], None, creds[1]) break if not auth: auth = self.netrc.authenticators(host) if auth: return 'Basic %s' % (base64.b64encode('%s:%s' % (auth[0], auth[2]))) return None # Backwards compatibility just in case somebody imports this outside of # depot_tools.
def test_domain_match(self): from cookielib import domain_match, user_domain_match self.assertTrue(domain_match("192.168.1.1", "192.168.1.1")) self.assertFalse(domain_match("192.168.1.1", ".168.1.1")) self.assertTrue(domain_match("x.y.com", "x.Y.com")) self.assertTrue(domain_match("x.y.com", ".Y.com")) self.assertFalse(domain_match("x.y.com", "Y.com")) self.assertTrue(domain_match("a.b.c.com", ".c.com")) self.assertFalse(domain_match(".c.com", "a.b.c.com")) self.assertTrue(domain_match("example.local", ".local")) self.assertFalse(domain_match("blah.blah", "")) self.assertFalse(domain_match("", ".rhubarb.rhubarb")) self.assertTrue(domain_match("", "")) self.assertTrue(user_domain_match("acme.com", "acme.com")) self.assertFalse(user_domain_match("acme.com", ".acme.com")) self.assertTrue(user_domain_match("rhubarb.acme.com", ".acme.com")) self.assertTrue(user_domain_match("www.rhubarb.acme.com", ".acme.com")) self.assertTrue(user_domain_match("x.y.com", "x.Y.com")) self.assertTrue(user_domain_match("x.y.com", ".Y.com")) self.assertFalse(user_domain_match("x.y.com", "Y.com")) self.assertTrue(user_domain_match("y.com", "Y.com")) self.assertFalse(user_domain_match(".y.com", "Y.com")) self.assertTrue(user_domain_match(".y.com", ".Y.com")) self.assertTrue(user_domain_match("x.y.com", ".com")) self.assertFalse(user_domain_match("x.y.com", "com")) self.assertFalse(user_domain_match("x.y.com", "m")) self.assertFalse(user_domain_match("x.y.com", ".m")) self.assertFalse(user_domain_match("x.y.com", "")) self.assertFalse(user_domain_match("x.y.com", ".")) self.assertTrue(user_domain_match("192.168.1.1", "192.168.1.1")) # not both HDNs, so must string-compare equal to match self.assertFalse(user_domain_match("192.168.1.1", ".168.1.1")) self.assertFalse(user_domain_match("192.168.1.1", ".")) # empty string is a special case self.assertFalse(user_domain_match("192.168.1.1", ""))
def test_domain_match(self): from cookielib import domain_match, user_domain_match self.assertTrue(domain_match("192.168.1.1", "192.168.1.1")) self.assertTrue(not domain_match("192.168.1.1", ".168.1.1")) self.assertTrue(domain_match("x.y.com", "x.Y.com")) self.assertTrue(domain_match("x.y.com", ".Y.com")) self.assertTrue(not domain_match("x.y.com", "Y.com")) self.assertTrue(domain_match("a.b.c.com", ".c.com")) self.assertTrue(not domain_match(".c.com", "a.b.c.com")) self.assertTrue(domain_match("example.local", ".local")) self.assertTrue(not domain_match("blah.blah", "")) self.assertTrue(not domain_match("", ".rhubarb.rhubarb")) self.assertTrue(domain_match("", "")) self.assertTrue(user_domain_match("acme.com", "acme.com")) self.assertTrue(not user_domain_match("acme.com", ".acme.com")) self.assertTrue(user_domain_match("rhubarb.acme.com", ".acme.com")) self.assertTrue(user_domain_match("www.rhubarb.acme.com", ".acme.com")) self.assertTrue(user_domain_match("x.y.com", "x.Y.com")) self.assertTrue(user_domain_match("x.y.com", ".Y.com")) self.assertTrue(not user_domain_match("x.y.com", "Y.com")) self.assertTrue(user_domain_match("y.com", "Y.com")) self.assertTrue(not user_domain_match(".y.com", "Y.com")) self.assertTrue(user_domain_match(".y.com", ".Y.com")) self.assertTrue(user_domain_match("x.y.com", ".com")) self.assertTrue(not user_domain_match("x.y.com", "com")) self.assertTrue(not user_domain_match("x.y.com", "m")) self.assertTrue(not user_domain_match("x.y.com", ".m")) self.assertTrue(not user_domain_match("x.y.com", "")) self.assertTrue(not user_domain_match("x.y.com", ".")) self.assertTrue(user_domain_match("192.168.1.1", "192.168.1.1")) # not both HDNs, so must string-compare equal to match self.assertTrue(not user_domain_match("192.168.1.1", ".168.1.1")) self.assertTrue(not user_domain_match("192.168.1.1", ".")) # empty string is a special case self.assertTrue(not user_domain_match("192.168.1.1", ""))
def _get_auth_for_host(self, host): for domain, creds in self.gitcookies.iteritems(): if cookielib.domain_match(host, domain): return (creds[0], None, creds[1]) return self.netrc.authenticators(host)