summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEric V. Smith <eric@trueblade.com>2014-04-14 16:58:07 (GMT)
committerEric V. Smith <eric@trueblade.com>2014-04-14 16:58:07 (GMT)
commitebdaaf40875fb8e29c7fbad5cc67221123995a8e (patch)
tree4feedc5959563cc17fdb855348951a88e7c4c598
parent6b2308803506321019bfe2ec1f2303ebb97ec998 (diff)
downloadcpython-ebdaaf40875fb8e29c7fbad5cc67221123995a8e.zip
cpython-ebdaaf40875fb8e29c7fbad5cc67221123995a8e.tar.gz
cpython-ebdaaf40875fb8e29c7fbad5cc67221123995a8e.tar.bz2
Issue #20480: Add ipaddress.reverse_pointer. Patch by Leon Weber.
-rw-r--r--Doc/library/ipaddress.rst15
-rw-r--r--Lib/ipaddress.py29
-rw-r--r--Lib/test/test_ipaddress.py8
-rw-r--r--Misc/ACKS1
-rw-r--r--Misc/NEWS2
5 files changed, 55 insertions, 0 deletions
diff --git a/Doc/library/ipaddress.rst b/Doc/library/ipaddress.rst
index 9625e71..0bc27ff 100644
--- a/Doc/library/ipaddress.rst
+++ b/Doc/library/ipaddress.rst
@@ -146,6 +146,20 @@ write code that handles both IP versions correctly.
the appropriate length (most significant octet first). This is 4 bytes
for IPv4 and 16 bytes for IPv6.
+ .. attribute:: reverse_pointer
+
+ The name of the reverse DNS PTR record for the IP address, e.g.::
+
+ >>> ipaddress.ip_address("127.0.0.1").reverse_pointer
+ '1.0.0.127.in-addr.arpa'
+ >>> ipaddress.ip_address("2001:db8::1").reverse_pointer
+ '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'
+
+ This is the name that could be used for performing a PTR lookup, not the
+ resolved hostname itself.
+
+ .. versionadded:: 3.5
+
.. attribute:: is_multicast
``True`` if the address is reserved for multicast use. See
@@ -226,6 +240,7 @@ write code that handles both IP versions correctly.
:class:`IPv4Address` class:
.. attribute:: packed
+ .. attribute:: reverse_pointer
.. attribute:: version
.. attribute:: max_prefixlen
.. attribute:: is_multicast
diff --git a/Lib/ipaddress.py b/Lib/ipaddress.py
index 54df39a..1e451f8 100644
--- a/Lib/ipaddress.py
+++ b/Lib/ipaddress.py
@@ -436,6 +436,17 @@ class _IPAddressBase(_TotalOrderingMixin):
return str(self)
@property
+ def reverse_pointer(self):
+ """The name of the reverse DNS pointer for the IP address, e.g.:
+ >>> ipaddress.ip_address("127.0.0.1").reverse_pointer
+ '1.0.0.127.in-addr.arpa'
+ >>> ipaddress.ip_address("2001:db8::1").reverse_pointer
+ '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'
+
+ """
+ return self._reverse_pointer()
+
+ @property
def version(self):
msg = '%200s has no version specified' % (type(self),)
raise NotImplementedError(msg)
@@ -1221,6 +1232,15 @@ class _BaseV4:
return True
return False
+ def _reverse_pointer(self):
+ """Return the reverse DNS pointer name for the IPv4 address.
+
+ This implements the method described in RFC1035 3.5.
+
+ """
+ reverse_octets = str(self).split('.')[::-1]
+ return '.'.join(reverse_octets) + '.in-addr.arpa'
+
@property
def max_prefixlen(self):
return self._max_prefixlen
@@ -1784,6 +1804,15 @@ class _BaseV6:
return '%s/%d' % (':'.join(parts), self._prefixlen)
return ':'.join(parts)
+ def _reverse_pointer(self):
+ """Return the reverse DNS pointer name for the IPv6 address.
+
+ This implements the method described in RFC3596 2.5.
+
+ """
+ reverse_chars = self.exploded[::-1].replace(':', '')
+ return '.'.join(reverse_chars) + '.ip6.arpa'
+
@property
def max_prefixlen(self):
return self._max_prefixlen
diff --git a/Lib/test/test_ipaddress.py b/Lib/test/test_ipaddress.py
index f2947b9..33adb3b 100644
--- a/Lib/test/test_ipaddress.py
+++ b/Lib/test/test_ipaddress.py
@@ -1593,6 +1593,14 @@ class IpaddrUnitTest(unittest.TestCase):
addr3.exploded)
self.assertEqual('192.168.178.1', addr4.exploded)
+ def testReversePointer(self):
+ addr1 = ipaddress.IPv4Address('127.0.0.1')
+ addr2 = ipaddress.IPv6Address('2001:db8::1')
+ self.assertEqual('1.0.0.127.in-addr.arpa', addr1.reverse_pointer)
+ self.assertEqual('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',
+ addr2.reverse_pointer)
+
def testIntRepresentation(self):
self.assertEqual(16909060, int(self.ipv4_address))
self.assertEqual(42540616829182469433547762482097946625,
diff --git a/Misc/ACKS b/Misc/ACKS
index 463d752..d3866fd 100644
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -1392,6 +1392,7 @@ Bob Watson
David Watson
Aaron Watters
Henrik Weber
+Leon Weber
Corran Webster
Glyn Webster
Phil Webster
diff --git a/Misc/NEWS b/Misc/NEWS
index 01ad416..d23f24b 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -37,6 +37,8 @@ Core and Builtins
- Issue #12546: Allow \x00 to be used as a fill character when using str, int,
float, and complex __format__ methods.
+- Issue #20480: Add ipaddress.reverse_pointer. Patch by Leon Weber.
+
Library
-------