Why does getaddrinfo not return all IP Addresses?
<p>I am trying to get all IP Addresses for: earth.all.vpn.airdns.org
In Python: </p>

<pre><code>def hostnamesToIps(hostnames):
  ip_list = []

  for hostname in hostnames:
      try:
          ais = socket.getaddrinfo(hostname, None)
          for result in ais:
              #print (result[4][0])
              ip_list.append(result[-1][0])
      except:
          eprint("Unable to get IP for hostname: " + hostname)

return list(set(ip_list))
</code></pre>

<p>(fyi eprint is a function for printing error). 
The Output gives me 29 Addresses.</p>

<p>But when i do:</p>

<pre><code>nslookup earth.all.vpn.airdns.org
</code></pre>

<p>I get about 100 entrys.</p>

<p>How to achieve this in python? Why i am not getting all entrys with "getaddrinfo"?</p>

<p>This behaviour only appears using windows (python 3). When i am executing the code on my Linux (python 2.7) it gives me the same result as using nslookup.</p>

<p><strong>info:</strong> As in the answer explained it does not depend on the system.</p>

<hr>

<p>Without changing anything the results of nslookup and getaddrinfo are the same now.</p>

<p>Tools like <code>dig</code>, <code>host</code> and <code>nslookup</code> query the default DNS server directly using UDP/TCP and their own implementation of DNS queries, whereas Python's <code>socket</code> module uses the DNS lookup interface of the operating system, which usually uses a more sophisticates lookup mechanism involving e.g. a dns cache, a host file, domain suffixes, link-local name resolution etc.</p>

<p><code>strace</code> shows that Python's <code>socket.getaddrinfo</code> ends up using  a netlink (AF_NETLINK) socket, to query the system for the DNS lookup (Python 2.7 on Ubuntu 12.04). <code>nslookup</code> however, reads the default DNS server from <code>/etc/resolv.conf</code> and opens a UDP socket on port 53.</p>

<p>I think there are two reason why you are getting a different entry count:</p>

<ol>
<li>The DNS entries are quite volatile a may change at any instant</li>
<li>Python returns cached entries provided by the system's DNS cache whereas <code>nslookup</code> always retrieves "fresh" results.</li>
</ol>

<p>Furthermore, <code>nslookup</code> might produce slightly different DNS queries than the system resolver (producing another answer). That could be checked with Wireshark, but I will leave that for now.</p>

<p><strike>Another issue could be the truncation of DNS responses when UPD is used. If there is a large number of entries, they won't fit in a single UDP package, so the answer contains a truncation flag. It depends on the client to re-send the DNS query over a TCP socket in order to retrieve all results.</strike> (Truncated answers are actually empty).</p>

<p><strong>Edit:</strong> A note on caching / volatile</p>

<p>Even if the mismatch is not due to your local DNS cache, it may be due to server-side caching. I tried several DNS servers and all gave different results for that particular name. That means that they are not in sync due to DNS changes within the time-to-live (TTL). </p>

python sockets