Accepts an IPv4 dotted quad and returns a string representing its binary
    counterpart
761337
def _ipv4_to_bits(ipaddr):
    '''
    
    '''
    return ''.join([bin(int(x))[2:].rjust(8, '0') for x in ipaddr.split('.')])
348608
def ip2hex(ip):
    '''
    
    '''
    parts = ip.split(".")
    if len(parts) != 4: return None
    ipv = 0
    for part in parts:
        try:
            p = int(part)
            if p < 0 or p > 255: return None
            ipv = (ipv << 8) + p
        except:
            return None
    return ipv
947205
def target_to_ipv4_cidr(target):
    

    splitted = target.split('/')
    if len(splitted) != 2:
        return None
    try:
        start_packed = inet_pton(socket.AF_INET, splitted[0])
        block = int(splitted[1])
    except (socket.error, ValueError):
        return None
    if block <= 0 or block > 30:
        return None
    start_value = int(binascii.hexlify(start_packed), 16) >> (32 - block)
    start_value = (start_value << (32 - block)) + 1
    end_value = (start_value | (0xffffffff >> block)) - 1
    start_packed = struct.pack('!I', start_value)
    end_packed = struct.pack('!I', end_value)
    return ipv4_range_to_list(start_packed, end_packed)
484617
def netmask_to_cidr (dq):
  
  if isinstance(dq, str):
    dq = IPv4Address(dq)
  v = int(dq)
  c = 0
  while v & 0x80000000:
    c += 1
    v <<= 1
  v = v & 0xffFFffFF
  if v != 0:
    raise RuntimeError("Netmask %s is not CIDR-compatible" % (dq,))
  return c
840592
function  WriteIpAddr (buffer, offset, ipstring) {
    var addr= ipstring.split('.');
    buffer.writeUInt8(parseInt(addr[3]), offset);
    buffer.writeUInt8(parseInt(addr[2]), offset + 1);
    buffer.writeUInt8(parseInt(addr[1]), offset + 2);
    buffer.writeUInt8(parseInt(addr[0]), offset + 3);
}
301676
def i2osp(self, long_integer, block_size):
        ''
        hex_string = '%X' % long_integer
        if len(hex_string) > 2 * block_size:
            raise ValueError('integer %i too large to encode in %i octets' % (long_integer, block_size))
        return a2b_hex(hex_string.zfill(2 * block_size))
225953
def ipv6_to_str(ip):
    
    if isinstance(ip, numbers.Integral):
        return addrconv.ipv6.bin_to_text(type_desc.Int16.from_user(ip))
    else:
        return addrconv.ipv6.bin_to_text(ip)
749816
private static byte[] parseDottedQuad(String address) {
		String[] splitString = address.split("\\.");

		if (splitString.length == 4) {

			int[] ints = new int[4];
			byte[] bytes = new byte[4];
			for (int i=0; i<4; i++) {
				ints[i] = Integer.parseInt(splitString[i]);
				if (ints[i] < 0 || ints[i] > 255) {
					throw new IllegalArgumentException("Invalid ip4Address or netmask");
				}
				bytes[i] = toByte(ints[i]); //a pox on java and it's signed bytes
			}

			return bytes;
		}
		else {
			throw new IllegalArgumentException("Address must be in dotted quad notation");
		}
	}
560739
def set_ipv4(self, ip):
        
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        bin_ip = socket.inet_aton(ip)
        ifreq = struct.pack('16sH2s4s8s', self.name, socket.AF_INET, '\x00'*2, bin_ip, '\x00'*8)
        fcntl.ioctl(sock, self.SIOCSIFADDR, ifreq)
        ifreq = struct.pack('16sH', self.name, self.IFF_UP|self.IFF_POINTOPOINT|self.IFF_RUNNING|self.IFF_MULTICAST)
        fcntl.ioctl(sock, self.SIOCSIFFLAGS, ifreq)
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
978031
def addr_info(addr):
    

    # If it's a string, it's in the UNIX family
    if isinstance(addr, basestring):
        return socket.AF_UNIX

    # Verify that addr is a tuple
    if not isinstance(addr, collections.Sequence):
        raise ValueError("address is not a tuple")

    # Make sure it has at least 2 fields
    if len(addr) < 2:
        raise ValueError("cannot understand address")

    # Sanity-check the port number
    if not (0 <= addr[1] < 65536):
        raise ValueError("cannot understand port number")

    # OK, first field should be an IP address; suck it out...
    ipaddr = addr[0]

    # Empty string means IPv4
    if not ipaddr:
        if len(addr) != 2:
            raise ValueError("cannot understand address")

        return socket.AF_INET

    # See if it's valid...
    if netaddr.valid_ipv6(ipaddr):
        if len(addr) > 4:
            raise ValueError("cannot understand address")

        return socket.AF_INET6
    elif netaddr.valid_ipv4(ipaddr):
        if len(addr) != 2:
            raise ValueError("cannot understand address")

        return socket.AF_INET

    raise ValueError("cannot understand address")
769521
def get_ipv4(hostname):
    
    addrinfo = socket.getaddrinfo(hostname, None, socket.AF_INET,
                                  socket.SOCK_STREAM)
    return [addrinfo[x][4][0] for x in range(len(addrinfo))]
47854
public static Inet4Address forIPv4String(String ipString) {
        requireNonNull(ipString);

        try{
            InetAddress parsed = forString(ipString);

            if (parsed instanceof Inet4Address)
                return (Inet4Address) parsed;

        } catch(Exception ignored){}

        throw new IllegalArgumentException(format("Invalid IPv4 representation: %s", ipString));
    }
140915
def address_in_network(ip, net):
    
    ipaddr = struct.unpack('=L', socket.inet_aton(ip))[0]
    netaddr, bits = net.split('/')
    netmask = struct.unpack('=L', socket.inet_aton(dotted_netmask(int(bits))))[0]
    network = struct.unpack('=L', socket.inet_aton(netaddr))[0] & netmask
    return (ipaddr & netmask) == (network & netmask)
356381
private static CharSequence convertReverseDNSIPv4(String str, int suffixStartIndex) throws AddressStringException {
		StringBuilder builder = new StringBuilder(suffixStartIndex);
		int segCount = 0;
		int j = suffixStartIndex;
		for(int i = suffixStartIndex - 1; i > 0; i--) {
			char c1 = str.charAt(i);
			if(c1 == IPv4Address.SEGMENT_SEPARATOR) {
				if(j - i <= 1) {
					throw new AddressStringException(str, i);
				}
				for(int k = i + 1; k < j; k++) {
					builder.append(str.charAt(k));
				}
				builder.append(c1);
				j = i;
				segCount++;
			}
		}
		for(int k = 0; k < j; k++) {
			builder.append(str.charAt(k));
		}
		if(segCount + 1 != IPv4Address.SEGMENT_COUNT) {
			throw new AddressStringException(str, 0);
		}
		return builder;
	}
742726
def _inet6_ntop(addr):
    
    # IPv6 addresses have 128bits (16 bytes)
    if len(addr) != 16:
        raise ValueError("invalid length of packed IP address string")

    # Decode to hex representation
    address = ":".join(plain_str(bytes_hex(addr[idx:idx + 2])).lstrip('0') or '0'  # noqa: E501
                       for idx in range(0, 16, 2))

    try:
        # Get the longest set of zero blocks. We need to take a look
        # at group 1 regarding the length, as 0:0:1:0:0:2:3:4 would
        # have two matches: 0:0: and :0:0: where the latter is longer,
        # though the first one should be taken. Group 1 is in both
        # cases 0:0.
        match = max(_IP6_ZEROS.finditer(address),
                    key=lambda m: m.end(1) - m.start(1))
        return '{}::{}'.format(address[:match.start()], address[match.end():])
    except ValueError:
        return address
1084229
def from_string(address):
		
		address = address.split('.')
		if len(address) != WIPV4Address.octet_count:
			raise ValueError('Invalid ip address: %s' % address)

		result = WIPV4Address()
		for i in range(WIPV4Address.octet_count):
			result.__address[i] = WBinArray(int(address[i]), WFixedSizeByteArray.byte_size)
		return result