def bypass_proxies(url, no_proxy): """Returns whether we should bypass proxies or not.""" parsed = urlparse(url) bypass = False if no_proxy == "": return False no_proxy = (host for host in no_proxy.replace(' ', '').split(',') if host) host_with_port = parsed.hostname if parsed.port: host_with_port += ':{0}'.format(parsed.port) chars = ("*", "*.", ".") for host in no_proxy: if host == "*": bypass = True if host == parsed.hostname or host == host_with_port: bypass = True if host.startswith(chars): host = host.lstrip('*') host = host.lstrip('.') if parsed.hostname.endswith(host) or host_with_port.endswith(host): bypass = True if is_ipv4_address(parsed.hostname): if is_valid_cidr(host): if address_in_network(host_with_port, host): bypass = True return bypass
def isInNet(host, pattern, mask): """ Pattern and mask specification is done the same way as for SOCKS configuration. :param str host: a DNS hostname, or IP address. If a hostname is passed, it will be resolved into an IP address by this function. :param str pattern: an IP address pattern in the dot-separated format :param str mask: mask for the IP address pattern informing which parts of the IP address should be matched against. 0 means ignore, 255 means match. :returns: True iff the IP address of the host matches the specified IP address pattern. :rtype: bool """ host_ip = host if is_ipv4_address(host) else dnsResolve(host) if not host_ip or not is_ipv4_address(pattern) or not is_ipv4_address(mask): return False return _address_in_network(host_ip, pattern, mask)
def isInNet(host, pattern, mask): """ Pattern and mask specification is done the same way as for SOCKS configuration. :param str host: a DNS hostname, or IP address. If a hostname is passed, it will be resolved into an IP address by this function. :param str pattern: an IP address pattern in the dot-separated format :param str mask: mask for the IP address pattern informing which parts of the IP address should be matched against. 0 means ignore, 255 means match. :returns: True iff the IP address of the host matches the specified IP address pattern. :rtype: bool """ host_ip = host if is_ipv4_address(host) else dnsResolve(host) if not host_ip or not is_ipv4_address(pattern) or not is_ipv4_address( mask): return False return _address_in_network(host_ip, pattern, mask)
def test_is_ipv4_address(self): from requests.utils import is_ipv4_address assert is_ipv4_address("8.8.8.8") assert not is_ipv4_address("8.8.8.8.8") assert not is_ipv4_address("localhost.localdomain")
def test_is_ipv4_address(self): from requests.utils import is_ipv4_address assert is_ipv4_address('8.8.8.8') assert not is_ipv4_address('8.8.8.8.8') assert not is_ipv4_address('localhost.localdomain')
def test_invalid(self, value): assert not is_ipv4_address(value)
def test_valid(self): assert is_ipv4_address('8.8.8.8')
def get_remote_host(self): forwarded_for = self.request.get("HTTP_X_FORWARDED_FOR") if is_ipv4_address(forwarded_for): return forwarded_for return self.request.get("REMOTE_ADDR")