Python ipaddr.IPv4Address() Examples
The following are 13
code examples of ipaddr.IPv4Address().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
ipaddr
, or try the search function
.
Example #1
Source File: common_util.py From encrypted-bigquery-client with Apache License 2.0 | 6 votes |
def FormatPackedIP(packed_ip): """Formats packed binary data to a readable ip address. Args: packed_ip: The packed binary data to be converted. Returns: A readable ip address. Returns: bigquery_client.BigqueryInvalidQueryError: If the address is not valid. """ packed_ip = ipaddr.Bytes(str(packed_ip)) try: ip_address = ipaddr.IPv4Address(packed_ip) return str(ip_address) except ipaddr.AddressValueError as e: pass try: ip_address = ipaddr.IPv6Address(packed_ip) return str(ip_address) except ipaddr.AddressValueError as e: raise bigquery_client.BigqueryInvalidQueryError(e, None, None, None)
Example #2
Source File: common_util.py From encrypted-bigquery-client with Apache License 2.0 | 6 votes |
def ParsePackedIP(readable_ip): try: ip_address = ipaddr.IPv4Address(readable_ip) return str(ipaddr.v4_int_to_packed(int(ip_address))) except ValueError: pass try: ip_address = ipaddr.IPv6Address(readable_ip) return str(ipaddr.v6_int_to_packed(int(ip_address))) except ValueError: raise bigquery_client.BigqueryInvalidQueryError( 'Invalid readable ip.', None, None, None) # TODO(user): Implement all URL functions. # Supported URL functions.
Example #3
Source File: url_helpers.py From n6 with GNU Affero General Public License v3.0 | 5 votes |
def _convert_ipv4_to_ipv6_suffix(ipv6_suffix_in_ipv4_format): """ >>> _convert_ipv4_to_ipv6_suffix('192.168.0.1') 'c0a8:0001' """ as_ipv4 = ipaddr.IPv4Address(ipv6_suffix_in_ipv4_format) as_int = int(as_ipv4) as_ipv6 = ipaddr.IPv6Address(as_int) ipv6_suffix = as_ipv6.exploded[-9:] assert _LAST_9_CHARS_OF_EXPLODED_IPV6_REGEX.search(ipv6_suffix) return ipv6_suffix
Example #4
Source File: spf.py From mailin with MIT License | 5 votes |
def set_ip(self, i): "Set connect ip, and ip6 or ip4 mode." self.iplist = False if i.lower() == 'list': self.iplist = [] ip6 = False elif i.lower() == 'list6': self.iplist = [] ip6 = True else: try: self.ipaddr = ipaddress.ip_address(i) except AttributeError: self.ipaddr = ipaddress.IPAddress(i) if self.ipaddr.version == 6: if self.ipaddr.ipv4_mapped: self.ipaddr = ipaddress.IPv4Address(self.ipaddr.ipv4_mapped) ip6 = False else: ip6 = True else: ip6 = False self.c = str(self.ipaddr) # NOTE: self.A is not lowercase, so isn't a macro. See query.expand() if ip6: self.A = 'AAAA' self.v = 'ip6' self.i = '.'.join(list(self.ipaddr.exploded.replace(':','').upper())) self.cidrmax = 128 else: self.A = 'A' self.v = 'in-addr' self.i = self.ipaddr.exploded self.cidrmax = 32
Example #5
Source File: ccp_util.py From ciscoconfparse with GNU General Public License v3.0 | 5 votes |
def ip(self): """Returns the address as an IPv4Address object.""" return self.ip_object
Example #6
Source File: ccp_util.py From ciscoconfparse with GNU General Public License v3.0 | 5 votes |
def netmask(self): """Returns the network mask as an IPv4Address object.""" return self.network_object.netmask
Example #7
Source File: ccp_util.py From ciscoconfparse with GNU General Public License v3.0 | 5 votes |
def broadcast(self): """Returns the broadcast address as an IPv4Address object.""" if sys.version_info[0] < 3: return self.network_object.broadcast else: return self.network_object.broadcast_address
Example #8
Source File: ccp_util.py From ciscoconfparse with GNU General Public License v3.0 | 5 votes |
def hostmask(self): """Returns the host mask as an IPv4Address object.""" return self.network_object.hostmask
Example #9
Source File: common_util.py From encrypted-bigquery-client with Apache License 2.0 | 5 votes |
def FormatIP(packed_ip): try: ip_address = ipaddr.IPv4Address(packed_ip) except ipaddr.AddressValueError as e: raise bigquery_client.BigqueryInvalidQueryError(e, None, None, None) return str(ip_address)
Example #10
Source File: common_util.py From encrypted-bigquery-client with Apache License 2.0 | 5 votes |
def ParseIP(readable_ip): try: ip_address = ipaddr.IPv4Address(readable_ip) except ipaddr.AddressValueError as e: raise bigquery_client.BigqueryInvalidQueryError(e, None, None, None) return int(ip_address)
Example #11
Source File: bytes_utils.py From p4runtime-shell with Apache License 2.0 | 5 votes |
def ipv4Addr_to_bytes(addr): try: ip = IPv4Address(addr) except AddressValueError: raise UserBadIPv4Error(addr) return ip.packed
Example #12
Source File: geoip.py From threatshell with BSD 3-Clause "New" or "Revised" License | 4 votes |
def as_lookup(self, args): if not isinstance(args, list): args = [args] docs = [] for arg in args: as_string = None successful = True doc = None if re.match("\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}", arg): as_string = self.g1_asnum_reader.org_by_addr(arg) start, end = self.g1_asnum_reader.range_by_ip(arg) start = ipaddr.IPv4Address(start) end = ipaddr.IPv4Address(end) net_range = ipaddr.summarize_address_range(start, end) doc = geo_docs.GeoIpASNDoc() setattr(doc, "ip_allocation", str(net_range[0])) else: as_string = self.g1_asnum_reader.org_by_name(arg) doc = geo_docs.GeoASNDoc() if as_string: as_parts = as_string.split(" ") setattr(doc, "as_num", as_parts[0]) setattr(doc, "as_name", " ".join(as_parts[1:])) setattr(doc, "successful", True) else: setattr(doc, "as_num", 0) setattr(doc, "as_name", "") setattr(doc, "successful", False) docs.append(doc) return docs
Example #13
Source File: ccp_util.py From ciscoconfparse with GNU General Public License v3.0 | 4 votes |
def __init__(self, arg="127.0.0.1/32", strict=False): # RGX_IPV4ADDR = re.compile(r'^(\d+\.\d+\.\d+\.\d+)') # RGX_IPV4ADDR_NETMASK = re.compile(r'(\d+\.\d+\.\d+\.\d+)\s+(\d+\.\d+\.\d+\.\d+)') self.arg = arg self.dna = "IPv4Obj" try: mm = _RGX_IPV4ADDR_NETMASK.search(arg) except TypeError: if getattr(arg, "dna", "") == "IPv4Obj": ip_str = "{0}/{1}".format(str(arg.ip_object), arg.prefixlen) self.network_object = IPv4Network(ip_str, strict=False) self.ip_object = IPv4Address(str(arg.ip_object)) return None elif isinstance(arg, IPv4Network): self.network_object = arg self.ip_object = IPv4Address(str(arg).split("/")[0]) return None elif isinstance(arg, IPv4Address): self.network_object = IPv4Network(str(arg) + "/32") self.ip_object = IPv4Address(str(arg).split("/")[0]) return None elif isinstance(arg, int): self.ip_object = IPv4Address(arg) self.network_object = IPv4Network( str(self.ip_object) + "/32", strict=False ) return None else: raise ValueError( "IPv4Obj doesn't understand how to parse {0}".format(arg) ) ERROR = "IPv4Obj couldn't parse '{0}'".format(arg) assert not (mm is None), ERROR mm_result = mm.groupdict() addr = ( mm_result["addr0"] or mm_result["addr1"] or mm_result["addr2"] or "127.0.0.1" ) ## Normalize addr if we get zero-padded strings, i.e. 172.001.001.001 addr = ".".join([str(int(ii)) for ii in addr.split(".")]) masklen = int(mm_result["masklen"] or 32) netmask = mm_result["netmask"] if netmask: ## ALWAYS check for the netmask first self.network_object = IPv4Network( "{0}/{1}".format(addr, netmask), strict=strict ) self.ip_object = IPv4Address("{0}".format(addr)) else: self.network_object = IPv4Network( "{0}/{1}".format(addr, masklen), strict=strict ) self.ip_object = IPv4Address("{0}".format(addr))