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

Delta Between Two Patch Sets: Lib/ipaddress.py

Issue 20480: Add ipaddress property to get reverse DNS name
Left Patch Set: Created 5 years, 9 months ago
Right Patch Set: Created 5 years, 9 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:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « Doc/library/ipaddress.rst ('k') | Lib/test/test_ipaddress.py » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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 418 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 437
438 @property 438 @property
439 def reverse_name(self): 439 def reverse_pointer(self):
440 """The reverse DNS name of the IP address, e.g.: 440 """The name of the reverse DNS pointer for the IP address, e.g.:
441 >>> ipaddress.ip_address("127.0.0.1").reverse_name 441 >>> ipaddress.ip_address("127.0.0.1").reverse_pointer
442 '1.0.0.127.in-addr.arpa.' 442 '1.0.0.127.in-addr.arpa'
443 >>> ipaddress.ip_address("2001:db8::1").reverse_name 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.' 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 """ 445
446 return self._reverse_name() 446 """
447 return self._reverse_pointer()
447 448
448 @property 449 @property
449 def version(self): 450 def version(self):
450 msg = '%200s has no version specified' % (type(self),) 451 msg = '%200s has no version specified' % (type(self),)
451 raise NotImplementedError(msg) 452 raise NotImplementedError(msg)
452 453
453 def _check_int_address(self, address): 454 def _check_int_address(self, address):
454 if address < 0: 455 if address < 0:
455 msg = "%d (< 0) is not permitted as an IPv%d address" 456 msg = "%d (< 0) is not permitted as an IPv%d address"
456 raise AddressValueError(msg % (address, self._version)) 457 raise AddressValueError(msg % (address, self._version))
(...skipping 712 matching lines...) Expand 10 before | Expand all | Expand 10 after
1169 try: 1170 try:
1170 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]
1171 except ValueError: 1172 except ValueError:
1172 return False 1173 return False
1173 if len(parts) != len(bits): 1174 if len(parts) != len(bits):
1174 return False 1175 return False
1175 if parts[0] < parts[-1]: 1176 if parts[0] < parts[-1]:
1176 return True 1177 return True
1177 return False 1178 return False
1178 1179
1179 def _reverse_name(self): 1180 def _reverse_pointer(self):
1180 """Return the reverse DNS name for the IPv4 address. 1181 """Return the reverse DNS pointer name for the IPv4 address.
1181 1182
1182 This implements the method described in RFC1035 3.5. 1183 This implements the method described in RFC1035 3.5.
1183 1184
1184 """ 1185 """
1185 rev_octets = str(self).split('.')[::-1] 1186 reverse_octets = str(self).split('.')[::-1]
1186 return '.'.join(rev_octets) + '.in-addr.arpa.' 1187 return '.'.join(reverse_octets) + '.in-addr.arpa'
1187 1188
1188 @property 1189 @property
1189 def max_prefixlen(self): 1190 def max_prefixlen(self):
1190 return self._max_prefixlen 1191 return self._max_prefixlen
1191 1192
1192 @property 1193 @property
1193 def version(self): 1194 def version(self):
1194 return self._version 1195 return self._version
1195 1196
1196 1197
(...skipping 561 matching lines...) Expand 10 before | Expand all | Expand 10 after
1758 else: 1759 else:
1759 ip_str = str(self) 1760 ip_str = str(self)
1760 1761
1761 ip_int = self._ip_int_from_string(ip_str) 1762 ip_int = self._ip_int_from_string(ip_str)
1762 hex_str = '%032x' % ip_int 1763 hex_str = '%032x' % ip_int
1763 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)]
1764 if isinstance(self, (_BaseNetwork, IPv6Interface)): 1765 if isinstance(self, (_BaseNetwork, IPv6Interface)):
1765 return '%s/%d' % (':'.join(parts), self._prefixlen) 1766 return '%s/%d' % (':'.join(parts), self._prefixlen)
1766 return ':'.join(parts) 1767 return ':'.join(parts)
1767 1768
1768 def _reverse_name(self): 1769 def _reverse_pointer(self):
1769 """Return the reverse DNS name for the IPv6 address. 1770 """Return the reverse DNS pointer name for the IPv6 address.
1770 1771
1771 This implements the method described in RFC3596 2.5. 1772 This implements the method described in RFC3596 2.5.
1772 1773
1773 """ 1774 """
1774 reverse_chars = self.exploded[::-1].replace(':', '') 1775 reverse_chars = self.exploded[::-1].replace(':', '')
1775 return '.'.join(reverse_chars) + '.ip6.arpa.' 1776 return '.'.join(reverse_chars) + '.ip6.arpa'
1776 1777
1777 @property 1778 @property
1778 def max_prefixlen(self): 1779 def max_prefixlen(self):
1779 return self._max_prefixlen 1780 return self._max_prefixlen
1780 1781
1781 @property 1782 @property
1782 def version(self): 1783 def version(self):
1783 return self._version 1784 return self._version
1784 1785
1785 1786
(...skipping 386 matching lines...) Expand 10 before | Expand all | Expand 10 after
2172 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.
2173 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
2174 addresses as defined by RFC 4193. 2175 addresses as defined by RFC 4193.
2175 2176
2176 Returns: 2177 Returns:
2177 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.
2178 2179
2179 """ 2180 """
2180 return (self.network_address.is_site_local and 2181 return (self.network_address.is_site_local and
2181 self.broadcast_address.is_site_local) 2182 self.broadcast_address.is_site_local)
LEFTRIGHT

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