From: Thomas Ward Date: Mon, 9 Mar 2020 20:58:11 +0000 (-0400) Subject: Use dns.reversename, extend reverse_lookup args X-Git-Tag: v2.0.0rc1~337^2~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c0126bc8982ef167a3f540593b7c29bb19507114;p=thirdparty%2Fdnspython.git Use dns.reversename, extend reverse_lookup args Use inbuilt dns.reversename. Extend the self.query argument bits into the reverse_lookup (tcp, source, raise_on_no_answer, source_port, lifetime), and force-define the rdtype and rdclass. --- diff --git a/dns/resolver.py b/dns/resolver.py index ead0ed53..3ecb6111 100644 --- a/dns/resolver.py +++ b/dns/resolver.py @@ -18,7 +18,6 @@ """DNS stub resolver.""" from urllib.parse import urlparse -import ipaddress import socket import sys import time @@ -1031,30 +1030,25 @@ class Resolver(object): self.cache.put((_qname, rdtype, rdclass), answer) return answer - def reverse_lookup(self, ipaddr): + def reverse_lookup(self, ipaddr, tcp=False, source=None, + raise_on_no_answer=True, source_port=0, + lifetime=None): """Use a resolver to run a Reverse IP Lookup for PTR records. This utilizes the in-built query function to perform a PTR lookup and tests to make sure that the entered string is in fact an IP address. - This utilizes the in-built ipaddress library for Python to validate that - the address is an IPv4 or IPv6 address, and errors if the specified - value is not a valid IPv4 or IPv6 address. It also uses the same library - to get the in-addr.arpa string to query to get the PTR record. - *ipaddr*, a ``str``, the IP address you want to get the PTR record for. """ - try: - ip = ipaddress.IPv4Address(ipaddr) - except ipaddress.AddressValueError: - try: - ip = ipaddress.IPv6Address(ipaddr) - except ipaddress.AddressValueError: - raise ipaddress.AddressValueError("The specified string does not " - "appear to be any known type of" - "IP Address.") - return self.query(ip.reverse_pointer, dns.rdatatype.PTR) + return self.query(dns.reversename.from_address(address), + rdtype=dns.rdatatype.PTR, + rdclass=dns.rdataclass.IN, + tcp=tcp, + source=source, + raise_on_no_answer=raise_on_no_answer, + source_port=source_port, + lifetime=lifetime) def use_tsig(self, keyring, keyname=None, algorithm=dns.tsig.default_algorithm):