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 />
• The system on which your <strong>Python</strong> script is running already knows about the local<br />
domain nameservers, thanks either to manual intervention by your system<br />
administrator or a network configuration protocol like DHCP in your <strong>of</strong>fice, home,<br />
or c<strong>of</strong>fee shop. To crank up DNS right inside your <strong>Python</strong> program, you will have<br />
to learn how to query your particular operating system for this information—an<br />
operating-system-specific action that we will not be covering in this book.<br />
• If you do not use the local DNS server, then you will not be able to benefit from its<br />
own cache that would prevent your application and other applications running on<br />
the same network from repeating requests about a hostname that is in frequent<br />
use at your location.<br />
• From time to time, adjustments are made to the world DNS infrastructure, and<br />
operating system libraries and daemons are gradually updated to accommodate<br />
this. If your program makes raw DNS calls <strong>of</strong> its own, then you will have to follow<br />
these changes yourself and make sure that your code stays up-to-date with the<br />
latest changes in TLD server IP addresses, conventions involving<br />
internationalization, and tweaks to the DNS protocol itself.<br />
Finally, note that <strong>Python</strong> does not come with any DNS facilities built into the Standard Library. If<br />
you are going to talk DNS using <strong>Python</strong>, then you must choose and learn a third-party library for doing<br />
so.<br />
Why to Use DNS<br />
There is, however, a solid and legitimate reason to make a DNS call from <strong>Python</strong>: because you are a mail<br />
server, or at the very least a client trying to send mail directly to your recipients without needing to run a<br />
local mail relay, and you want to look up the MX records associated with a domain so that you can find<br />
the correct mail server for your friends at @example.com.<br />
So we are going to go ahead and take a look at one <strong>of</strong> the third-party DNS libraries for <strong>Python</strong> as we<br />
bring this chapter to its close. There are at least two good ones available for <strong>Python</strong> at the moment. They<br />
are available for quick installation into a virtual environment if you want to try them out. (See Chapter 1<br />
to remember how to use virtualenv and pip.)<br />
We will focus on the slightly more popular distribution, pydns, which descends from a DNS module<br />
first written by Guido van Rossum, which at least gives it a glow <strong>of</strong> historical legitimacy. It makes a DNS<br />
package available for you to import. Its competitor, the dnspython distribution, creates a lower-case dns<br />
package instead, just so you can keep things straight! Both distributions have seen updates within the<br />
past year, as <strong>of</strong> this writing—in fact, as I type this in September 2010, I can see that both packages were<br />
updated within a few days <strong>of</strong> each other back in January 2010.<br />
Note that neither project provides code that knows how to “start from scratch” and begin a query<br />
with a search <strong>of</strong> the Internet root domain nameservers! Instead, each library uses its own tricks to find<br />
out what domain nameservers your Windows or POSIX operating system is currently using, and then<br />
asks those servers to go do recursive queries on its behalf. So not a single piece <strong>of</strong> code in this chapter<br />
avoids needing to have a correctly configured host which an administrator or network configuration<br />
service has already configured with working nameservers.<br />
Since both are on the <strong>Python</strong> Package Index, you can install and try one <strong>of</strong> them out like this:<br />
$ pip install pydns<br />
Your <strong>Python</strong> interpreter will then gain the ability to run our first DNS program listing, shown in<br />
Listing 4–3. Neither package seems to have any real documentation, so you will have to start with what is<br />
shown here and extrapolate by reading whatever example code you can find on the Web.<br />
66