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

Side by Side Diff: Lib/ipaddress.py

Issue 20480: Add ipaddress property to get reverse DNS name
Patch Set: Created 5 years, 10 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_pointer(self):
440 """The name of the reverse DNS pointer for the IP address, e.g.:
441 >>> ipaddress.ip_address("127.0.0.1").reverse_pointer
442 '1.0.0.127.in-addr.arpa'
443 >>> ipaddress.ip_address("2001:db8::1").reverse_pointer
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 """
447 return self._reverse_pointer()
437 448
438 @property 449 @property
439 def version(self): 450 def version(self):
440 msg = '%200s has no version specified' % (type(self),) 451 msg = '%200s has no version specified' % (type(self),)
441 raise NotImplementedError(msg) 452 raise NotImplementedError(msg)
442 453
443 def _check_int_address(self, address): 454 def _check_int_address(self, address):
444 if address < 0: 455 if address < 0:
445 msg = "%d (< 0) is not permitted as an IPv%d address" 456 msg = "%d (< 0) is not permitted as an IPv%d address"
446 raise AddressValueError(msg % (address, self._version)) 457 raise AddressValueError(msg % (address, self._version))
(...skipping 711 matching lines...) Expand 10 before | Expand all | Expand 10 after
1158 bits = ip_str.split('.') 1169 bits = ip_str.split('.')
1159 try: 1170 try:
1160 parts = [x for x in map(int, bits) if x in self._valid_mask_octets] 1171 parts = [x for x in map(int, bits) if x in self._valid_mask_octets]
1161 except ValueError: 1172 except ValueError:
1162 return False 1173 return False
1163 if len(parts) != len(bits): 1174 if len(parts) != len(bits):
1164 return False 1175 return False
1165 if parts[0] < parts[-1]: 1176 if parts[0] < parts[-1]:
1166 return True 1177 return True
1167 return False 1178 return False
1179
1180 def _reverse_pointer(self):
1181 """Return the reverse DNS pointer name for the IPv4 address.
1182
1183 This implements the method described in RFC1035 3.5.
1184
1185 """
1186 reverse_octets = str(self).split('.')[::-1]
1187 return '.'.join(reverse_octets) + '.in-addr.arpa'
1168 1188
1169 @property 1189 @property
1170 def max_prefixlen(self): 1190 def max_prefixlen(self):
1171 return self._max_prefixlen 1191 return self._max_prefixlen
1172 1192
1173 @property 1193 @property
1174 def version(self): 1194 def version(self):
1175 return self._version 1195 return self._version
1176 1196
1177 1197
(...skipping 560 matching lines...) Expand 10 before | Expand all | Expand 10 after
1738 ip_str = str(self.ip) 1758 ip_str = str(self.ip)
1739 else: 1759 else:
1740 ip_str = str(self) 1760 ip_str = str(self)
1741 1761
1742 ip_int = self._ip_int_from_string(ip_str) 1762 ip_int = self._ip_int_from_string(ip_str)
1743 hex_str = '%032x' % ip_int 1763 hex_str = '%032x' % ip_int
1744 parts = [hex_str[x:x+4] for x in range(0, 32, 4)] 1764 parts = [hex_str[x:x+4] for x in range(0, 32, 4)]
1745 if isinstance(self, (_BaseNetwork, IPv6Interface)): 1765 if isinstance(self, (_BaseNetwork, IPv6Interface)):
1746 return '%s/%d' % (':'.join(parts), self._prefixlen) 1766 return '%s/%d' % (':'.join(parts), self._prefixlen)
1747 return ':'.join(parts) 1767 return ':'.join(parts)
1768
1769 def _reverse_pointer(self):
1770 """Return the reverse DNS pointer name for the IPv6 address.
1771
1772 This implements the method described in RFC3596 2.5.
1773
1774 """
1775 reverse_chars = self.exploded[::-1].replace(':', '')
1776 return '.'.join(reverse_chars) + '.ip6.arpa'
1748 1777
1749 @property 1778 @property
1750 def max_prefixlen(self): 1779 def max_prefixlen(self):
1751 return self._max_prefixlen 1780 return self._max_prefixlen
1752 1781
1753 @property 1782 @property
1754 def version(self): 1783 def version(self):
1755 return self._version 1784 return self._version
1756 1785
1757 1786
(...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. 2173 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 2174 Use is_private to test if this address is in the space of unique local
2146 addresses as defined by RFC 4193. 2175 addresses as defined by RFC 4193.
2147 2176
2148 Returns: 2177 Returns:
2149 A boolean, True if the address is reserved per RFC 3513 2.5.6. 2178 A boolean, True if the address is reserved per RFC 3513 2.5.6.
2150 2179
2151 """ 2180 """
2152 return (self.network_address.is_site_local and 2181 return (self.network_address.is_site_local and
2153 self.broadcast_address.is_site_local) 2182 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+