import dns
import dns.resolver
import dns.rdatatype
from typing import Union, List


class DNSResolver(dns.resolver.Resolver):

    def __init__(self, filename='/etc/resolv.conf', configure=False,
                 nameservers: Union[str, List[str]] = None):
        # Run the dns.resolver.Resolver superclass init call to configure
        # the object. Then, depending on the value in configure argument,
        # do something with the nameservers argument, which is unique to this
        # class object instead.
        super(DNSResolver, self).__init__(filename, configure)

        if not configure:
            if isinstance(nameservers, str):
                self.nameservers = [nameservers]
            elif isinstance(nameservers, list):
                self.nameservers = nameservers
            else:
                self.nameservers = ['8.8.8.8, 8.8.4.4']


def dns_resolve(domain: str, resolver: DNSResolver = DNSResolver(configure=True)) -> list:
    addrs = []

    for answer in resolver.query(domain, 'A').response.answer:
        for item in answer:
            addrs.append(item.address)

    for answer in resolver.query(domain, 'AAAA').response.answer:
        for item in answer:
            addrs.append(item.address)

    return addrs

import dns
import dns.resolver
import dns.rdatatype
from typing import Union, List


class DNSResolver(dns.resolver.Resolver):

    def __init__(self, filename='/etc/resolv.conf', configure=False,
                 nameservers: Union[str, List[str]] = None):
        # Run the dns.resolver.Resolver superclass init call to configure
        # the object. Then, depending on the value in configure argument,
        # do something with the nameservers argument, which is unique to this
        # class object instead.
        super(DNSResolver, self).__init__(filename, configure)

        if not configure:
            if isinstance(nameservers, str):
                self.nameservers = [nameservers]
            elif isinstance(nameservers, list):
                self.nameservers = nameservers
            else:
                self.nameservers = ['8.8.8.8, 8.8.4.4']


def dns_resolve(domain: str, resolver: DNSResolver = DNSResolver(configure=True)) -> list:
    addrs = []

    try:
        for answer in resolver.query(domain, 'A').response.answer:
            for item in answer:
                if item.rdtype == dns.rdatatype.A:
                    addrs.append(item.address)
    except dns.resolver.NoAnswer:
        pass

    try:
        for answer in resolver.query(domain, 'AAAA').response.answer:
            for item in answer:
                if item.rdtype == dns.rdatatype.AAAA:
                    addrs.append(item.address)
    except dns.resolver.NoAnswer:
        pass

    return addrs

Implement rdatatype-aware and NoAnswer-aware DNS handling
Implement rdatatype-aware and NoAnswer-aware DNS handling

This will work for CNAME entries because CNAMEs hit by A or AAAA lookups behave like `dig` does - they will trigger a second resultset for the CNAME entry in order to return the IP address.

This also is amended to handle a "NoAnswer" response - i.e. if there are no IPv4 or IPv6 addresses for a given CNAME or records lookup.  The list will therefore have all the CNAME-resolved IP addresses as independent strings.

  import dns
  import dns.resolver
  import dns.rdatatype
  from typing import Union, List
  
  
  class DNSResolver(dns.resolver.Resolver):
  
      def __init__(self, filename='/etc/resolv.conf', configure=False,
                   nameservers: Union[str, List[str]] = None):
          # Run the dns.resolver.Resolver superclass init call to configure
          # the object. Then, depending on the value in configure argument,
          # do something with the nameservers argument, which is unique to this
          # class object instead.
          super(DNSResolver, self).__init__(filename, configure)
  
          if not configure:
              if isinstance(nameservers, str):
                  self.nameservers = [nameservers]
              elif isinstance(nameservers, list):
                  self.nameservers = nameservers
              else:
                  self.nameservers = ['8.8.8.8, 8.8.4.4']
  
  
  def dns_resolve(domain: str, resolver: DNSResolver = DNSResolver(configure=True)) -> list:
      addrs = []
  
+     try:
-     for answer in resolver.query(domain, 'A').response.answer:
+         for answer in resolver.query(domain, 'A').response.answer:
-         for item in answer:
+             for item in answer:
+                 if item.rdtype == dns.rdatatype.A:
-             addrs.append(item.address)
+                     addrs.append(item.address)
+     except dns.resolver.NoAnswer:
+         pass
  
+     try:
-     for answer in resolver.query(domain, 'AAAA').response.answer:
+         for answer in resolver.query(domain, 'AAAA').response.answer:
-         for item in answer:
+             for item in answer:
+                 if item.rdtype == dns.rdatatype.AAAA:
-             addrs.append(item.address)
+                     addrs.append(item.address)
+     except dns.resolver.NoAnswer:
+         pass
  
      return addrs
  
Implement rdatatype-aware and NoAnswer-aware DNS handling
## Code Before:
import dns
import dns.resolver
import dns.rdatatype
from typing import Union, List


class DNSResolver(dns.resolver.Resolver):

    def __init__(self, filename='/etc/resolv.conf', configure=False,
                 nameservers: Union[str, List[str]] = None):
        # Run the dns.resolver.Resolver superclass init call to configure
        # the object. Then, depending on the value in configure argument,
        # do something with the nameservers argument, which is unique to this
        # class object instead.
        super(DNSResolver, self).__init__(filename, configure)

        if not configure:
            if isinstance(nameservers, str):
                self.nameservers = [nameservers]
            elif isinstance(nameservers, list):
                self.nameservers = nameservers
            else:
                self.nameservers = ['8.8.8.8, 8.8.4.4']


def dns_resolve(domain: str, resolver: DNSResolver = DNSResolver(configure=True)) -> list:
    addrs = []

    for answer in resolver.query(domain, 'A').response.answer:
        for item in answer:
            addrs.append(item.address)

    for answer in resolver.query(domain, 'AAAA').response.answer:
        for item in answer:
            addrs.append(item.address)

    return addrs

## Instruction:
Implement rdatatype-aware and NoAnswer-aware DNS handling
## Code After:
import dns
import dns.resolver
import dns.rdatatype
from typing import Union, List


class DNSResolver(dns.resolver.Resolver):

    def __init__(self, filename='/etc/resolv.conf', configure=False,
                 nameservers: Union[str, List[str]] = None):
        # Run the dns.resolver.Resolver superclass init call to configure
        # the object. Then, depending on the value in configure argument,
        # do something with the nameservers argument, which is unique to this
        # class object instead.
        super(DNSResolver, self).__init__(filename, configure)

        if not configure:
            if isinstance(nameservers, str):
                self.nameservers = [nameservers]
            elif isinstance(nameservers, list):
                self.nameservers = nameservers
            else:
                self.nameservers = ['8.8.8.8, 8.8.4.4']


def dns_resolve(domain: str, resolver: DNSResolver = DNSResolver(configure=True)) -> list:
    addrs = []

    try:
        for answer in resolver.query(domain, 'A').response.answer:
            for item in answer:
                if item.rdtype == dns.rdatatype.A:
                    addrs.append(item.address)
    except dns.resolver.NoAnswer:
        pass

    try:
        for answer in resolver.query(domain, 'AAAA').response.answer:
            for item in answer:
                if item.rdtype == dns.rdatatype.AAAA:
                    addrs.append(item.address)
    except dns.resolver.NoAnswer:
        pass

    return addrs
