Check if the IP address is valid and routable
    Return either True or False
1724
def _valid_ip(ip_address):
    '''
    
    '''

    try:
        address = ipaddress.IPv4Address(ip_address)
    except ipaddress.AddressValueError:
        return False

    if address.is_unspecified or \
        address.is_loopback or \
        address.is_link_local or \
        address.is_multicast or \
        address.is_reserved:
        return False

    return True
383959
def validate(self, ip, **kwargs):
        """"""
        
        if ip is None:
            return False

        ip = stringify(ip)

        if self.IPV4_REGEX.match(ip):

            try:
                socket.inet_pton(socket.AF_INET, ip)
                return True
            except AttributeError:  # no inet_pton here, sorry
                try:
                    socket.inet_aton(ip)
                except socket.error:
                    return False
                return ip.count('.') == 3
            except socket.error:  # not a valid address
                return False

        if self.IPV6_REGEX.match(ip):
            try:
                socket.inet_pton(socket.AF_INET6, ip)
            except socket.error:  # not a valid address
                return False
            return True
979919
def isValidPublicAddress(address: str) -> bool:
    """"""
    valid = False

    if len(address) == 34 and address[0] == 'A':
        try:
            base58.b58decode_check(address.encode())
            valid = True
        except ValueError:
            # checksum mismatch
            valid = False

    return valid
512548
public static boolean isValidArg(String arg) {
    final String[] args = arg.split(":");
    if (args.length != 2) {
      return false;
    }

    final String ip = args[1];
    return InetAddresses.isInetAddress(ip);
  }
239794
def dshield_ip_check(ip):
    """"""
    if not is_IPv4Address(ip):
        return None

    headers = {'User-Agent': useragent}
    url = 'https://isc.sans.edu/api/ip/'
    response = requests.get('{0}{1}?json'.format(url, ip), headers=headers)
    return response.json()
803541
def check_ip(ip, log=False):
        """"""
        if log:
            print('Checking ip: {}...'.format(ip))

        request_timeout = 0.1

        try:
            tv_url = 'http://{}:6095/request?action=isalive'.format(ip)
            request = requests.get(tv_url, timeout=request_timeout)
        except requests.exceptions.ConnectTimeout:
            return False

        return request.status_code == 200
302829
def connection_check(addr, port=80, safe=False, ipv6=None):
    '''
    
    '''
    return dns_check(addr, port, safe, ipv6)
713136
def valid_ipv4(ip):
    
    match =  _valid_ipv4.match(ip)
    if match is None:
        return False

    octets  = match.groups()
    if len(octets) != 4:
        return False

    first = int(octets[0])
    if first < 1 or first > 254:
        return False

    for i in range(1, 4):
        octet = int(octets[i])
        if octet < 0 or octet > 255:
            return False

    return True
738325
def valid_server?(server, port = SSL_PORT)
      # cheating by shelling out to curl, because getting open-uri, or even net/http to use
      # ssl_options like :OP_NO_SSLv2 and :OP_NO_SSLv3 will take time to figure out..
      return true if `/usr/bin/curl -s 'https://#{server}:#{port}/#{TEST_PATH}'`.include? TEST_CONTENT
      return true if `/usr/bin/curl -s 'http://#{server}:#{port}/#{TEST_PATH}'`.include? TEST_CONTENT
      false

      # # try ssl first
      # # NOTE:  doesn't work if we can't disallow SSLv3 or force TLSv1
      # # See cheat above.
      # begin
      #   return true if open("https://#{server}:#{port}/#{TEST_PATH}", ssl_verify_mode: OpenSSL::SSL::VERIFY_NONE).read.include? TEST_CONTENT
      #
      # rescue
      #   # then regular http
      #   begin
      #     return true if open("http://#{server}:#{port}/#{TEST_PATH}").read.include? TEST_CONTENT
      #   rescue
      #     # any errors = no API
      #     return false
      #   end # begin
      # end # begin
      # # if we're here, no API
      # false
    end
1024190
def check_valid_ip_or_cidr(val, return_as_cidr=False):
    
    is_ip = True
    if "/" in val:
        ip_check(val, netmask_expected=True)
        is_ip = False
    else:
        ip_check(val, netmask_expected=False)

    if return_as_cidr and is_ip:
        # Convert a plain IP to a CIDR
        if val == "0.0.0.0":
            # Special case for the default route
            val = "0.0.0.0/0"
        else:
            val = "%s/32" % val

    try:
        ipaddress.IPv4Network(unicode(val))
    except Exception as e:
        raise ArgsError("Not a valid network: %s" % str(e))

    return val
888292
def is_blocked(self, ip):
        """"""
        blocked = True

        if ip in self.allowed_admin_ips:
            blocked = False

        for allowed_range in self.allowed_admin_ip_ranges:
            if ipaddress.ip_address(ip) in ipaddress.ip_network(allowed_range):
                blocked = False

        return blocked
148819
def _is_valid_url(self, url):
        """"""
        try:
            r = requests.head(url, proxies=self.proxy_servers)
            value = r.status_code in [200]
        except Exception as error:
            logger.error(str(error))
            value = False

        return value
20348
def validipaddr(address):
    
    try:
        octets = address.split('.')
        if len(octets) != 4:
            return False
        for x in octets:
            if not (0 <= int(x) <= 255):
                return False
    except ValueError:
        return False
    return True
500507
def validate_host_ip(ips, subnets):
    
    # Make sure we prune ``None`` arguments
    subnets = [s for s in subnets if s is not None]
    validate_one_subnet = len(subnets) == 1

    def ip_in_one_subnet(ips, subnet):
        """ ensure an ip exists in at least one subnet """
        for ip in ips:
            if net.ip_in_subnet(ip, subnet):
                return True
        return False

    for subnet in subnets:
        if ip_in_one_subnet(ips, subnet):
            if validate_one_subnet:
                return
            else:  # keep going to make sure the other subnets are ok
                continue
        else:
            msg = "subnet (%s) is not valid for any of the ips found %s" % (subnet, str(ips))
            raise RuntimeError(msg)
448107
def determine_ip_address():
    """"""
    addrs = [
        x.address for k, v in psutil.net_if_addrs().items() if k[0] == "e"
        for x in v if x.family == AddressFamily.AF_INET
    ]
    return addrs[0]
96425
def ip(ip_addr, return_tuple=True):
    
    regex_ip = __re.compile("^((25[0-5])|(2[0-4][0-9])|(1[0-9][0-9])|([1-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|(1[0-9][0-9])|([1-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|(1[0-9][0-9])|([1-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|(1[0-9][0-9])|([1-9]?[0-9]))$")
    if return_tuple:
        while not regex_ip.match(ip_addr):
            print("Not a good IP.")
            print("Please try again.")
            ip_addr = input("Please enter a IP address in the following format x.x.x.x: ")
        return ip_addr
    elif not return_tuple:
        if not regex_ip.match(ip_addr):
            return False
        else:
            return True
1076948
def is_valid_ipv4 (ip):
    
    if not _ipv4_re.match(ip):
        return False
    a, b, c, d = [int(i) for i in ip.split(".")]
    return a <= 255 and b <= 255 and c <= 255 and d <= 255