09.11.2016 Views

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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CHAPTER 4 ■ SOCKET NAMES AND DNS<br />

Priority: 10 Hostname: pechora2.icann.org<br />

» Hostname pechora2.icann.org = A 192.0.43.8<br />

...<br />

Priority: 10 Hostname: pechora8.icann.org<br />

» Hostname pechora8.icann.org = A 192.0.43.8<br />

By trying this script against many different domains, you will be able to see how both big and small<br />

organizations arrange for incoming e-mails to be routed to IP addresses.<br />

Zeroconf and Dynamic DNS<br />

There are two last technologies that you are perhaps not likely to implement yourself, but that deserve a<br />

quick mention because they allow machines to find each other when they lack permanent and stable IP<br />

addresses.<br />

The Zeroconf standard combines three techniques so that computers thrown on to a network<br />

segment with each other can discover each other's presence and names without a network<br />

administrator needing to install and configure a DHCP and DNS server. Apple computers use this<br />

technology extensively to find adjacent machines and printers, Linux machines <strong>of</strong>ten run an avahi<br />

service that implements the protocol, and there is an old pyzeroconf project that <strong>of</strong>fers a complete<br />

<strong>Python</strong> implementation <strong>of</strong> the protocol suite. One <strong>of</strong> the technologies included in Zeroconf is “multicast<br />

DNS” (mDNS), which allows all <strong>of</strong> the machines on the local network to answer when another machine<br />

needs to look up a hostname.<br />

Dynamic DNS services are Internet sites built to serve users whose machines are regularly changing<br />

their IP address—perhaps because the address assigned by their ISP is not stable but is pulled from a<br />

pool <strong>of</strong> free addresses with every reconnect. By <strong>of</strong>fering an API through which the user can <strong>of</strong>fer her<br />

username, password, and new IP address, the DDNS service can update its database and point the user's<br />

domain name at the new IP. This technology was pioneered by the dyndns.com site, and it absolves the<br />

user <strong>of</strong> the need to rent and operate his or her own DNS server if he or she has only a few domain names<br />

to maintain. There appears to be a dyndnsc project on the Package Index that <strong>of</strong>fers a client that can<br />

communicate with DDNS services.<br />

Summary<br />

<strong>Python</strong> programs <strong>of</strong>ten have to turn hostnames into socket addresses to which they can actually make<br />

connections.<br />

Most hostname lookup should occur through the getsockaddr() function in the socket module,<br />

since its intelligence is usually supplied by your operating system and it will know not only how to look<br />

up domain names, but also what flavor <strong>of</strong> address the local IP stack is configured to support.<br />

Old IPv4 addresses are still the most prevalent on the Internet, but IPv6 is becoming more and more<br />

common. By deferring all hostname and port name lookup to getsockaddr(), your <strong>Python</strong> program can<br />

treat addresses as opaque strings and not have to worry about parsing or interpreting them.<br />

Behind most name resolution is the DNS, a worldwide distributed database that forwards domain<br />

name queries directly to the servers <strong>of</strong> the organization that owns a domain. While not <strong>of</strong>ten used<br />

directly from <strong>Python</strong>, it can be very helpful in determining where to direct e-mail based on the e-mail<br />

domain named after the @ sign in an e-mail address.<br />

70

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

Saved successfully!

Ooh no, something went wrong!