def connection_from_url(url, **kw): """ Given a url, return an :class:`.ConnectionPool` instance of its host. This is a shortcut for not having to parse out the scheme, host, and port of the url before creating an :class:`.ConnectionPool` instance. :param url: Absolute URL string that must include the scheme. Port is optional. :param \**kw: Passes additional parameters to the constructor of the appropriate :class:`.ConnectionPool`. Useful for specifying things like timeout, maxsize, headers, etc. Example: :: >>> conn = connection_from_url('http://google.com/') >>> r = conn.request('GET', '/') """ scheme, host, port = get_host(url) if scheme == 'https': return MyHTTPSConnectionPool(host, port=port, **kw) else: return HTTPConnectionPool(host, port=port, **kw)
def test_get_host(self): url_host_map = { # Hosts 'http://google.com/mail': ('http', 'google.com', None), 'http://google.com/mail/': ('http', 'google.com', None), 'google.com/mail': ('http', 'google.com', None), 'http://google.com/': ('http', 'google.com', None), 'http://google.com': ('http', 'google.com', None), 'http://www.google.com': ('http', 'www.google.com', None), 'http://mail.google.com': ('http', 'mail.google.com', None), 'http://google.com:8000/mail/': ('http', 'google.com', 8000), 'http://google.com:8000': ('http', 'google.com', 8000), 'https://google.com': ('https', 'google.com', None), 'https://google.com:8000': ('https', 'google.com', 8000), 'http://*****:*****@127.0.0.1:1234': ('http', '127.0.0.1', 1234), 'http://google.com/foo=http://bar:42/baz': ('http', 'google.com', None), 'http://google.com?foo=http://bar:42/baz': ('http', 'google.com', None), 'http://google.com#foo=http://bar:42/baz': ('http', 'google.com', None), # IPv4 '173.194.35.7': ('http', '173.194.35.7', None), 'http://173.194.35.7': ('http', '173.194.35.7', None), 'http://173.194.35.7/test': ('http', '173.194.35.7', None), 'http://173.194.35.7:80': ('http', '173.194.35.7', 80), 'http://173.194.35.7:80/test': ('http', '173.194.35.7', 80), # IPv6 '[2a00:1450:4001:c01::67]': ('http', '[2a00:1450:4001:c01::67]', None), 'http://[2a00:1450:4001:c01::67]': ('http', '[2a00:1450:4001:c01::67]', None), 'http://[2a00:1450:4001:c01::67]/test': ('http', '[2a00:1450:4001:c01::67]', None), 'http://[2a00:1450:4001:c01::67]:80': ('http', '[2a00:1450:4001:c01::67]', 80), 'http://[2a00:1450:4001:c01::67]:80/test': ('http', '[2a00:1450:4001:c01::67]', 80), # More IPv6 from http://www.ietf.org/rfc/rfc2732.txt 'http://[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:8000/index.html': ('http', '[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]', 8000), 'http://[1080:0:0:0:8:800:200C:417A]/index.html': ('http', '[1080:0:0:0:8:800:200C:417A]', None), 'http://[3ffe:2a00:100:7031::1]': ('http', '[3ffe:2a00:100:7031::1]', None), 'http://[1080::8:800:200C:417A]/foo': ('http', '[1080::8:800:200C:417A]', None), 'http://[::192.9.5.5]/ipng': ('http', '[::192.9.5.5]', None), 'http://[::FFFF:129.144.52.38]:42/index.html': ('http', '[::FFFF:129.144.52.38]', 42), 'http://[2010:836B:4179::836B:4179]': ('http', '[2010:836B:4179::836B:4179]', None), } for url, expected_host in url_host_map.items(): returned_host = get_host(url) self.assertEquals(returned_host, expected_host)
def test(args): log.setLevel(logging.DEBUG) log.info("starting test") if args.keytab: s4u2p.authGSSKeytab(args.keytab) scheme, host, port = get_host(args.url) p = KerberosConnectionPool(host=host, port=port, as_user=args.user, spn=args.spn) r=p.request("GET", args.url) print r.data
def __init__(self, host, port=None, strict=False, timeout=None, maxsize=1, block=False, headers=headers): try: host = get_host(host)[1] except TypeError: # Already a host-ified host pass headers = {k.lower(): v for (k, v) in headers.items()} HTTPConnectionPool.__init__(self, host, port, strict, timeout, maxsize, block, headers) self.cookie_session = CookieSession()
def test(args): log.setLevel(logging.DEBUG) log.info("starting test") if args.keytab: s4u2p.authGSSKeytab(args.keytab) scheme, host, port = get_host(args.url) p = KerberosConnectionPool(host=host, port=port, as_user=args.user, spn=args.spn) r = p.request("GET", args.url) print r.data
def get_domain(_url): try: return get_host(_url)[1] except: return reg_sess.sub( '', reg_query.sub( '', reg_hash.sub( '', reg_path.sub( '', reg_http.sub('', _url.lower()).strip('/')))).strip('/'))
def test_get_host(self): url_host_map = { # Hosts "http://google.com/mail": ("http", "google.com", None), "http://google.com/mail/": ("http", "google.com", None), "google.com/mail": ("http", "google.com", None), "http://google.com/": ("http", "google.com", None), "http://google.com": ("http", "google.com", None), "http://www.google.com": ("http", "www.google.com", None), "http://mail.google.com": ("http", "mail.google.com", None), "http://google.com:8000/mail/": ("http", "google.com", 8000), "http://google.com:8000": ("http", "google.com", 8000), "https://google.com": ("https", "google.com", None), "https://google.com:8000": ("https", "google.com", 8000), "http://*****:*****@127.0.0.1:1234": ("http", "127.0.0.1", 1234), "http://google.com/foo=http://bar:42/baz": ("http", "google.com", None), "http://google.com?foo=http://bar:42/baz": ("http", "google.com", None), "http://google.com#foo=http://bar:42/baz": ("http", "google.com", None), # IPv4 "173.194.35.7": ("http", "173.194.35.7", None), "http://173.194.35.7": ("http", "173.194.35.7", None), "http://173.194.35.7/test": ("http", "173.194.35.7", None), "http://173.194.35.7:80": ("http", "173.194.35.7", 80), "http://173.194.35.7:80/test": ("http", "173.194.35.7", 80), # IPv6 "[2a00:1450:4001:c01::67]": ("http", "[2a00:1450:4001:c01::67]", None), "http://[2a00:1450:4001:c01::67]": ("http", "[2a00:1450:4001:c01::67]", None), "http://[2a00:1450:4001:c01::67]/test": ("http", "[2a00:1450:4001:c01::67]", None), "http://[2a00:1450:4001:c01::67]:80": ("http", "[2a00:1450:4001:c01::67]", 80), "http://[2a00:1450:4001:c01::67]:80/test": ("http", "[2a00:1450:4001:c01::67]", 80), # More IPv6 from http://www.ietf.org/rfc/rfc2732.txt "http://[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:8000/index.html": ( "http", "[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]", 8000, ), "http://[1080:0:0:0:8:800:200C:417A]/index.html": ("http", "[1080:0:0:0:8:800:200C:417A]", None), "http://[3ffe:2a00:100:7031::1]": ("http", "[3ffe:2a00:100:7031::1]", None), "http://[1080::8:800:200C:417A]/foo": ("http", "[1080::8:800:200C:417A]", None), "http://[::192.9.5.5]/ipng": ("http", "[::192.9.5.5]", None), "http://[::FFFF:129.144.52.38]:42/index.html": ("http", "[::FFFF:129.144.52.38]", 42), "http://[2010:836B:4179::836B:4179]": ("http", "[2010:836B:4179::836B:4179]", None), } for url, expected_host in url_host_map.items(): returned_host = get_host(url) self.assertEquals(returned_host, expected_host)
def test_get_host(self): url_host_map = { # Hosts 'http://google.com/mail': ('http', 'google.com', None), 'http://google.com/mail/': ('http', 'google.com', None), 'google.com/mail': ('http', 'google.com', None), 'http://google.com/': ('http', 'google.com', None), 'http://google.com': ('http', 'google.com', None), 'http://www.google.com': ('http', 'www.google.com', None), 'http://mail.google.com': ('http', 'mail.google.com', None), 'http://google.com:8000/mail/': ('http', 'google.com', 8000), 'http://google.com:8000': ('http', 'google.com', 8000), 'https://google.com': ('https', 'google.com', None), 'https://google.com:8000': ('https', 'google.com', 8000), 'http://*****:*****@127.0.0.1:1234': ('http', '127.0.0.1', 1234), 'http://google.com/foo=http://bar:42/baz': ('http', 'google.com', None), 'http://google.com?foo=http://bar:42/baz': ('http', 'google.com', None), 'http://google.com#foo=http://bar:42/baz': ('http', 'google.com', None), # IPv4 '173.194.35.7': ('http', '173.194.35.7', None), 'http://173.194.35.7': ('http', '173.194.35.7', None), 'http://173.194.35.7/test': ('http', '173.194.35.7', None), 'http://173.194.35.7:80': ('http', '173.194.35.7', 80), 'http://173.194.35.7:80/test': ('http', '173.194.35.7', 80), # IPv6 '[2a00:1450:4001:c01::67]': ('http', '[2a00:1450:4001:c01::67]', None), 'http://[2a00:1450:4001:c01::67]': ('http', '[2a00:1450:4001:c01::67]', None), 'http://[2a00:1450:4001:c01::67]/test': ('http', '[2a00:1450:4001:c01::67]', None), 'http://[2a00:1450:4001:c01::67]:80': ('http', '[2a00:1450:4001:c01::67]', 80), 'http://[2a00:1450:4001:c01::67]:80/test': ('http', '[2a00:1450:4001:c01::67]', 80), # More IPv6 from http://www.ietf.org/rfc/rfc2732.txt 'http://[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:8000/index.html': ('http', '[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]', 8000), 'http://[1080:0:0:0:8:800:200C:417A]/index.html': ('http', '[1080:0:0:0:8:800:200C:417A]', None), 'http://[3ffe:2a00:100:7031::1]': ('http', '[3ffe:2a00:100:7031::1]', None), 'http://[1080::8:800:200C:417A]/foo': ('http', '[1080::8:800:200C:417A]', None), 'http://[::192.9.5.5]/ipng': ('http', '[::192.9.5.5]', None), 'http://[::FFFF:129.144.52.38]:42/index.html': ('http', '[::FFFF:129.144.52.38]', 42), 'http://[2010:836B:4179::836B:4179]': ('http', '[2010:836B:4179::836B:4179]', None), } for url, expected_host in url_host_map.items(): returned_host = get_host(url) self.assertEqual(returned_host, expected_host)
def test_get_host(self): url_host_map = { 'http://google.com/mail': ('http', 'google.com', None), 'http://google.com/mail/': ('http', 'google.com', None), 'google.com/mail': ('http', 'google.com', None), 'http://google.com/': ('http', 'google.com', None), 'http://google.com': ('http', 'google.com', None), 'http://www.google.com': ('http', 'www.google.com', None), 'http://mail.google.com': ('http', 'mail.google.com', None), 'http://google.com:8000/mail/': ('http', 'google.com', 8000), 'http://google.com:8000': ('http', 'google.com', 8000), 'https://google.com': ('https', 'google.com', None), 'https://google.com:8000': ('https', 'google.com', 8000), 'http://*****:*****@127.0.0.1:1234': ('http', '127.0.0.1', 1234), } for url, expected_host in url_host_map.items(): returned_host = get_host(url) self.assertEquals(returned_host, expected_host)
def test_get_host(self): url_host_map = { 'http://google.com/mail': ('http', 'google.com', None), 'http://google.com/mail/': ('http', 'google.com', None), 'google.com/mail': ('http', 'google.com', None), 'http://google.com/': ('http', 'google.com', None), 'http://google.com': ('http', 'google.com', None), 'http://www.google.com': ('http', 'www.google.com', None), 'http://mail.google.com': ('http', 'mail.google.com', None), 'http://google.com:8000/mail/': ('http', 'google.com', 8000), 'http://google.com:8000': ('http', 'google.com', 8000), 'https://google.com': ('https', 'google.com', None), 'https://google.com:8000': ('https', 'google.com', 8000), 'http://*****:*****@127.0.0.1:1234': ('http', '127.0.0.1', 1234), 'http://google.com/foo=http://bar:42/baz': ('http', 'google.com', None), 'http://google.com?foo=http://bar:42/baz': ('http', 'google.com', None), 'http://google.com#foo=http://bar:42/baz': ('http', 'google.com', None), } for url, expected_host in url_host_map.items(): returned_host = get_host(url) self.assertEquals(returned_host, expected_host)
def domain(url): url = reg_spots.sub('.', url) _domain = get_host(url)[1].strip('.').strip() ret_domain = reg_domain.findall(_domain) _domain = ret_domain[0] if ret_domain else '' return reg_not_en.sub('', _domain.lower())