Coonection test fails with DNS timeOut

mflix) F:\M220P\mflix-python>pytest -m connection
============================= test session starts =============================
platform win32 – Python 3.7.0, pytest-3.8.0, py-1.6.0, pluggy-0.7.1
rootdir: F:\M220P\mflix-python, inifile:
plugins: remotedata-0.3.0, openfiles-0.3.0, flask-0.10.0, doctestplus-0.1.3, arraydiff-0.2
collected 43 items / 39 deselected

tests\test_db_connection.py FFFF [100%]

================================== FAILURES ===================================
______________________________ test_atlas_setup _______________________________

hostname = ‘mflix-avw5t.mongodb.net

def _get_dns_txt_options(hostname):
    try:
      results = resolver.query(hostname, 'TXT')

C:\Users\Ashok\Anaconda3\lib\site-packages\pymongo\uri_parser.py:296:


qname = ‘mflix-avw5t.mongodb.net’, rdtype = ‘TXT’, rdclass = 1, tcp = False
source = None, raise_on_no_answer = True, source_port = 0

def query(qname, rdtype=dns.rdatatype.A, rdclass=dns.rdataclass.IN,
          tcp=False, source=None, raise_on_no_answer=True,
          source_port=0):
    """Query nameservers to find the answer to the question.

    This is a convenience function that uses the default resolver
    object to make the query.
    @see: L{dns.resolver.Resolver.query} for more information on the
    parameters."""
    return get_default_resolver().query(qname, rdtype, rdclass, tcp, source,
                                      raise_on_no_answer, source_port)

C:\Users\Ashok\Anaconda3\lib\site-packages\dns\resolver.py:1132:


self = <dns.resolver.Resolver object at 0x0000023376FE07F0>
qname = , rdtype = 16, rdclass = 1
tcp = False, source = None, raise_on_no_answer = True, source_port = 0

def query(self, qname, rdtype=dns.rdatatype.A, rdclass=dns.rdataclass.IN,
          tcp=False, source=None, raise_on_no_answer=True, source_port=0):
    """Query nameservers to find the answer to the question.

        The I{qname}, I{rdtype}, and I{rdclass} parameters may be objects
        of the appropriate type, or strings that can be converted into objects
        of the appropriate type.  E.g. For I{rdtype} the integer 2 and the
        the string 'NS' both mean to query for records with DNS rdata type NS.

        @param qname: the query name
        @type qname: dns.name.Name object or string
        @param rdtype: the query type
        @type rdtype: int or string
        @param rdclass: the query class
        @type rdclass: int or string
        @param tcp: use TCP to make the query (default is False).
        @type tcp: bool
        @param source: bind to this IP address (defaults to machine default
        IP).
        @type source: IP address in dotted quad notation
        @param raise_on_no_answer: raise NoAnswer if there's no answer
        (defaults is True).
        @type raise_on_no_answer: bool
        @param source_port: The port from which to send the message.
        The default is 0.
        @type source_port: int
        @rtype: dns.resolver.Answer instance
        @raises Timeout: no answers could be found in the specified lifetime
        @raises NXDOMAIN: the query name does not exist
        @raises YXDOMAIN: the query name is too long after DNAME substitution
        @raises NoAnswer: the response did not contain an answer and
        raise_on_no_answer is True.
        @raises NoNameservers: no non-broken nameservers are available to
        answer the question."""

    if isinstance(qname, string_types):
        qname = dns.name.from_text(qname, None)
    if isinstance(rdtype, string_types):
        rdtype = dns.rdatatype.from_text(rdtype)
    if dns.rdatatype.is_metatype(rdtype):
        raise NoMetaqueries
    if isinstance(rdclass, string_types):
        rdclass = dns.rdataclass.from_text(rdclass)
    if dns.rdataclass.is_metaclass(rdclass):
        raise NoMetaqueries
    qnames_to_try = []
    if qname.is_absolute():
        qnames_to_try.append(qname)
    else:
        if len(qname) > 1:
            qnames_to_try.append(qname.concatenate(dns.name.root))
        if self.search:
            for suffix in self.search:
                qnames_to_try.append(qname.concatenate(suffix))
        else:
            qnames_to_try.append(qname.concatenate(self.domain))
    all_nxdomain = True
    nxdomain_responses = {}
    start = time.time()
    _qname = None # make pylint happy
    for _qname in qnames_to_try:
        if self.cache:
            answer = self.cache.get((_qname, rdtype, rdclass))
            if answer is not None:
                if answer.rrset is None and raise_on_no_answer:
                    raise NoAnswer(response=answer.response)
                else:
                    return answer
        request = dns.message.make_query(_qname, rdtype, rdclass)
        if self.keyname is not None:
            request.use_tsig(self.keyring, self.keyname,
                             algorithm=self.keyalgorithm)
        request.use_edns(self.edns, self.ednsflags, self.payload)
        if self.flags is not None:
            request.flags = self.flags
        response = None
        #
        # make a copy of the servers list so we can alter it later.
        #
        nameservers = self.nameservers[:]
        errors = []
        if self.rotate:
            random.shuffle(nameservers)
        backoff = 0.10
        while response is None:
            if len(nameservers) == 0:
                raise NoNameservers(request=request, errors=errors)
            for nameserver in nameservers[:]:
                timeout = self._compute_timeout(start)
                port = self.nameserver_ports.get(nameserver, self.port)
                try:
                    tcp_attempt = tcp
                    if tcp:
                        response = dns.query.tcp(request, nameserver,
                                                 timeout, port,
                                                 source=source,
                                                 source_port=source_port)
                    else:
                        response = dns.query.udp(request, nameserver,
                                                 timeout, port,
                                                 source=source,
                                                 source_port=source_port)
                        if response.flags & dns.flags.TC:
                            # Response truncated; retry with TCP.
                            tcp_attempt = True
                            timeout = self._compute_timeout(start)
                            response = \
                                dns.query.tcp(request, nameserver,
                                              timeout, port,
                                              source=source,
                                              source_port=source_port)
                except (socket.error, dns.exception.Timeout) as ex:
                    #
                    # Communication failure or timeout.  Go to the
                    # next server
                    #
                    errors.append((nameserver, tcp_attempt, port, ex,
                                   response))
                    response = None
                    continue
                except dns.query.UnexpectedSource as ex:
                    #
                    # Who knows?  Keep going.
                    #
                    errors.append((nameserver, tcp_attempt, port, ex,
                                   response))
                    response = None
                    continue
                except dns.exception.FormError as ex:
                    #
                    # We don't understand what this server is
                    # saying.  Take it out of the mix and
                    # continue.
                    #
                    nameservers.remove(nameserver)
                    errors.append((nameserver, tcp_attempt, port, ex,
                                   response))
                    response = None
                    continue
                except EOFError as ex:
                    #
                    # We're using TCP and they hung up on us.
                    # Probably they don't support TCP (though
                    # they're supposed to!).  Take it out of the
                    # mix and continue.
                    #
                    nameservers.remove(nameserver)
                    errors.append((nameserver, tcp_attempt, port, ex,
                                   response))
                    response = None
                    continue
                rcode = response.rcode()
                if rcode == dns.rcode.YXDOMAIN:
                    ex = YXDOMAIN()
                    errors.append((nameserver, tcp_attempt, port, ex,
                                   response))
                    raise ex
                if rcode == dns.rcode.NOERROR or \
                        rcode == dns.rcode.NXDOMAIN:
                    break
                #
                # We got a response, but we're not happy with the
                # rcode in it.  Remove the server from the mix if
                # the rcode isn't SERVFAIL.
                #
                if rcode != dns.rcode.SERVFAIL or not self.retry_servfail:
                    nameservers.remove(nameserver)
                errors.append((nameserver, tcp_attempt, port,
                               dns.rcode.to_text(rcode), response))
                response = None
            if response is not None:
                break
            #
            # All nameservers failed!
            #
            if len(nameservers) > 0:
                #
                # But we still have servers to try.  Sleep a bit
                # so we don't pound them!
                #
              timeout = self._compute_timeout(start)

C:\Users\Ashok\Anaconda3\lib\site-packages\dns\resolver.py:1041:


self = <dns.resolver.Resolver object at 0x0000023376FE07F0>
start = 1543196082.270146

def _compute_timeout(self, start):
    now = time.time()
    duration = now - start
    if duration < 0:
        if duration < -1:
            # Time going backwards is bad.  Just give up.
            raise Timeout(timeout=duration)
        else:
            # Time went backwards, but only a little.  This can
            # happen, e.g. under vmware with older linux kernels.
            # Pretend it didn't happen.
            now = start
    if duration >= self.lifetime:
      raise Timeout(timeout=duration)

E dns.exception.Timeout: The DNS operation timed out after 30.014559030532837 seconds

Can anyone help me? Why DNS operation timed out is comming?

Well, I tried resolving that hostname and I didn’t get a result. Did you get it correctly from Atlas?

No still I am getting DNS timed out error

My previous post made a wrong assumption about the SRV records. This great post by @nayamama explains how the DNS records really work.

However, an incorrect hostname does not lead to dns timeouts. A time-out is caused when the dns server cannot be reached. Do the following work inside the VM?

nslookup www.google.com
nslookup MongoDB.net

Thanks this is very helpful