09.11.2016 Views

Foundations of Python Network Programming 978-1-4302-3004-5

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CHAPTER 4 ■ SOCKET NAMES AND DNS<br />

Expiration Date:28-Mar-2016 05:00:00 UTC<br />

...<br />

Registrant Name:<strong>Python</strong> S<strong>of</strong>tware Foundation<br />

...<br />

Name Server:NS2.XS4ALL.NL<br />

Name Server:NS.XS4ALL.NL<br />

And that provides our answer! Wherever you are in the world, your DNS request for any hostname<br />

within python.org must be passed on to one <strong>of</strong> the two DNS servers named in that entry. Of course,<br />

when your DNS server makes this request to a top-level domain nameserver, it does not really get back<br />

only two names like those just given; instead, it is also given their IP addresses so that it can contact<br />

them directly without incurring another round <strong>of</strong> DNS lookup.<br />

Your DNS server is now finished talking to both the root-level DNS server and the top-level .org<br />

DNS server, and can communicate directly with NS2.XS4ALL.NL or NS.XS4ALL.NL to ask about the<br />

python.org domain—and, in fact, it will usually try one <strong>of</strong> them and then fall back to trying the other if<br />

the first one is unavailable. This increases the chances <strong>of</strong> you getting an answer, but, <strong>of</strong> course, it can<br />

increase the amount <strong>of</strong> time that you sit staring at your web browser before the page can actually be<br />

displayed!<br />

Depending on how python.org has its nameservers configured, the DNS server might require just<br />

one more query to get its answer, or it might take several if the organization is a large one with many<br />

departments and sub-departments that all run their own DNS servers to which requests need to be<br />

delegated. In this case, the www.python.org query can be answered directly by either <strong>of</strong> the two servers<br />

just named, and your DNS server can now return a UDP packet to your browser telling it which IP<br />

addresses belong to that hostname.<br />

Note that this process required four separate network round-trips. Your machine made a request<br />

and got a response from your own DNS server, and in order to answer that request, your DNS server had<br />

to make a recursive query that consisted <strong>of</strong> three different round-trips to other servers. No wonder your<br />

browser sits there spinning when you enter a domain name for the first time!<br />

Why Not to Use DNS<br />

The foregoing explanation <strong>of</strong> a typical DNS query has, I hope, made clear that your operating system is<br />

doing a lot for you when you need a hostname looked up. For this reason, I am going to recommend<br />

that, unless you absolutely need to speak DNS for some quite particular reason, you always rely on<br />

getaddrinfo() or some other system-supported mechanism for resolving hostnames. Consider the<br />

benefits:<br />

• The DNS is <strong>of</strong>ten not the only way that a system gets name information. If your<br />

application runs <strong>of</strong>f and tries to use DNS on its own as its first choice for resolving<br />

a domain name, then users will notice that some computer names that work<br />

everywhere else on your system—in their browser, in file share names, and so<br />

forth—suddenly do not work when they use your application, because you are not<br />

deferring to mechanisms like WINS or /etc/hosts like the operating system itself<br />

does.<br />

• The local machine probably has a cache <strong>of</strong> recently queried domain names that<br />

might already know about the host whose IP address you need. If you try speaking<br />

DNS yourself to answer your query, you will be duplicating work that has already<br />

been done.<br />

65

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!