Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(253559)

Side by Side Diff: Lib/ipaddress.py

Issue 20480: Add ipaddress property to get reverse DNS name
Patch Set: Created 5 years, 6 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Doc/library/ipaddress.rst ('k') | Lib/test/test_ipaddress.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # Copyright 2007 Google Inc. 1 # Copyright 2007 Google Inc.
2 # Licensed to PSF under a Contributor Agreement. 2 # Licensed to PSF under a Contributor Agreement.
3 3
4 """A fast, lightweight IPv4/IPv6 manipulation library in Python. 4 """A fast, lightweight IPv4/IPv6 manipulation library in Python.
5 5
6 This library is used to create/poke/manipulate IPv4 and IPv6 addresses 6 This library is used to create/poke/manipulate IPv4 and IPv6 addresses
7 and networks. 7 and networks.
8 8
9 """ 9 """
10 10
(...skipping 416 matching lines...) Expand 10 before | Expand all | Expand 10 after
427 427
428 @property 428 @property
429 def exploded(self): 429 def exploded(self):
430 """Return the longhand version of the IP address as a string.""" 430 """Return the longhand version of the IP address as a string."""
431 return self._explode_shorthand_ip_string() 431 return self._explode_shorthand_ip_string()
432 432
433 @property 433 @property
434 def compressed(self): 434 def compressed(self):
435 """Return the shorthand version of the IP address as a string.""" 435 """Return the shorthand version of the IP address as a string."""
436 return str(self) 436 return str(self)
437
438 @property
439 def reverse_name(self):
440 """The reverse DNS name of the IP address, e.g.:
441 >>> ipaddress.ip_address("127.0.0.1").reverse_name
442 '1.0.0.127.in-addr.arpa.'
443 >>> ipaddress.ip_address("2001:db8::1").reverse_name
444 '1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.b.d.0.1.0.0.2.ip6 .arpa.'
445 """
446 return self._reverse_name()
437 447
438 @property 448 @property
439 def version(self): 449 def version(self):
440 msg = '%200s has no version specified' % (type(self),) 450 msg = '%200s has no version specified' % (type(self),)
441 raise NotImplementedError(msg) 451 raise NotImplementedError(msg)
442 452
443 def _check_int_address(self, address): 453 def _check_int_address(self, address):
444 if address < 0: 454 if address < 0:
445 msg = "%d (< 0) is not permitted as an IPv%d address" 455 msg = "%d (< 0) is not permitted as an IPv%d address"
446 raise AddressValueError(msg % (address, self._version)) 456 raise AddressValueError(msg % (address, self._version))
(...skipping 711 matching lines...) Expand 10 before | Expand all | Expand 10 after
1158 bits = ip_str.split('.') 1168 bits = ip_str.split('.')
1159 try: 1169 try:
1160 parts = [x for x in map(int, bits) if x in self._valid_mask_octets] 1170 parts = [x for x in map(int, bits) if x in self._valid_mask_octets]
1161 except ValueError: 1171 except ValueError:
1162 return False 1172 return False
1163 if len(parts) != len(bits): 1173 if len(parts) != len(bits):
1164 return False 1174 return False
1165 if parts[0] < parts[-1]: 1175 if parts[0] < parts[-1]:
1166 return True 1176 return True
1167 return False 1177 return False
1178
1179 def _reverse_name(self):
1180 """Return the reverse DNS name for the IPv4 address.
1181
1182 This implements the method described in RFC1035 3.5.
1183
1184 """
1185 rev_octets = str(self).split('.')[::-1]
1186 return '.'.join(rev_octets) + '.in-addr.arpa.'
1168 1187
1169 @property 1188 @property
1170 def max_prefixlen(self): 1189 def max_prefixlen(self):
1171 return self._max_prefixlen 1190 return self._max_prefixlen
1172 1191
1173 @property 1192 @property
1174 def version(self): 1193 def version(self):
1175 return self._version 1194 return self._version
1176 1195
1177 1196
(...skipping 560 matching lines...) Expand 10 before | Expand all | Expand 10 after
1738 ip_str = str(self.ip) 1757 ip_str = str(self.ip)
1739 else: 1758 else:
1740 ip_str = str(self) 1759 ip_str = str(self)
1741 1760
1742 ip_int = self._ip_int_from_string(ip_str) 1761 ip_int = self._ip_int_from_string(ip_str)
1743 hex_str = '%032x' % ip_int 1762 hex_str = '%032x' % ip_int
1744 parts = [hex_str[x:x+4] for x in range(0, 32, 4)] 1763 parts = [hex_str[x:x+4] for x in range(0, 32, 4)]
1745 if isinstance(self, (_BaseNetwork, IPv6Interface)): 1764 if isinstance(self, (_BaseNetwork, IPv6Interface)):
1746 return '%s/%d' % (':'.join(parts), self._prefixlen) 1765 return '%s/%d' % (':'.join(parts), self._prefixlen)
1747 return ':'.join(parts) 1766 return ':'.join(parts)
1767
1768 def _reverse_name(self):
1769 """Return the reverse DNS name for the IPv6 address.
1770
1771 This implements the method described in RFC3596 2.5.
1772
1773 """
1774 reverse_chars = self.exploded[::-1].replace(':', '')
1775 return '.'.join(reverse_chars) + '.ip6.arpa.'
1748 1776
1749 @property 1777 @property
1750 def max_prefixlen(self): 1778 def max_prefixlen(self):
1751 return self._max_prefixlen 1779 return self._max_prefixlen
1752 1780
1753 @property 1781 @property
1754 def version(self): 1782 def version(self):
1755 return self._version 1783 return self._version
1756 1784
1757 1785
(...skipping 386 matching lines...) Expand 10 before | Expand all | Expand 10 after
2144 Note that the site-local address space has been deprecated by RFC 3879. 2172 Note that the site-local address space has been deprecated by RFC 3879.
2145 Use is_private to test if this address is in the space of unique local 2173 Use is_private to test if this address is in the space of unique local
2146 addresses as defined by RFC 4193. 2174 addresses as defined by RFC 4193.
2147 2175
2148 Returns: 2176 Returns:
2149 A boolean, True if the address is reserved per RFC 3513 2.5.6. 2177 A boolean, True if the address is reserved per RFC 3513 2.5.6.
2150 2178
2151 """ 2179 """
2152 return (self.network_address.is_site_local and 2180 return (self.network_address.is_site_local and
2153 self.broadcast_address.is_site_local) 2181 self.broadcast_address.is_site_local)
OLDNEW
« no previous file with comments | « Doc/library/ipaddress.rst ('k') | Lib/test/test_ipaddress.py » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+