Foundations of Python Network Programming

(WallPaper) #1
Chapter 4 ■ SoCket NameS aNd dNS

69

Depending on how python.org has its name servers configured, the DNS server might require just one
more query to get its answer, or it might require several more queries if the organization is a large one with many
departments and subdepartments that all run their own DNS servers to which requests need to be delegated. In this
case, the http://www.python.org query can be answered directly by either of the two servers just named, and your DNS
server can now return a UDP packet to your browser telling it which IP addresses belong to that hostname.
Note that this process required four separate network round-trips. Your machine made a request and got a
response from your own DNS server, and in order to answer that request, your DNS server had to make a recursive
query that consisted of three different round-trips to other servers. No wonder your browser sits there spinning when
you enter a domain name for the first time.


Why Not to Use Raw DNS

The foregoing explanation of a typical DNS query has, I hope, made clear that your operating system is doing quite a
lot for you when you need a hostname looked up. For this reason, I am going to recommend that unless you absolutely
need to speak DNS for a very particular reason, you always rely on getaddrinfo() or some other system-supported
mechanism for resolving hostnames. Consider these benefits of letting your operating system look up names for you:


•    The DNS is often not the only way that a system gets name information. If your application
runs off and tries to use DNS on its own as its first choice for resolving a domain name, then
users will notice that some computer names that work everywhere else on your system—in
their browser, in file share paths, and so forth—suddenly do not work when they use your
application because you are not consulting mechanisms like WINS or /etc/hosts like the
operating system itself does.

•    The local machine probably has a cache of recently queried domain names that might already
contain the host whose IP address you need. If you try speaking DNS yourself to answer your
query, you will be duplicating work that has already been done.

•    The system on which your Python script is running already knows about the local domain
name servers, thanks either to manual configuration by your system administrator or to
a network setup protocol like DHCP. To crank up DNS inside your Python program, you
will have to learn how to query your particular operating system for this information—an
operating-system-specific action that I will not be covering in this book.

•    If you do not use the local DNS server, then you will not be able to benefit from its own cache
that would prevent your application and other applications running on the same network
from repeating requests about a hostname that is in frequent use at your location.

•    From time to time, adjustments are made to the world DNS infrastructure, and operating
system libraries and daemons are gradually updated to accommodate this. If your program
makes raw DNS calls of its own, then you will have to follow these changes yourself and
make sure your code stays up-to-date with the latest changes in TLD server IP addresses,
conventions involving internationalization, and tweaks to the DNS protocol itself.

Finally, note that Python does not come with any DNS facilities built into the Standard Library. If you are going to
talk DNS using Python, then you must choose and learn a third-party library for doing so.


Making a DNS Query from Python

There is, however, a solid and legitimate reason to make a DNS call from Python. It is because you are a mail server,
or at the least a client trying to send mail directly to your recipients without needing to run a local mail relay, and you
want to look up the MX records associated with a domain so that you can find the correct mail server for your friends
at @example.com.

Free download pdf