Foundations of Python Network Programming

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

73

Whether that IP belongs to one machine or is shared by a cluster of hosts, is, of course, something that you
cannot easily see from outside. Other organizations are more aggressive in giving incoming e-mails several places to
land. The IANA currently has no fewer than six e-mail servers (or, at least it offers six IP addresses with which you can
connect to, however many servers it in fact is running).


$ python dns_mx.py iana.org
This domain has 6 MX records
Priority 10
pechora7.icann.org has A address 192.0.46.73
Priority 10
pechora5.icann.org has A address 192.0.46.71
Priority 10
pechora8.icann.org has A address 192.0.46.74
Priority 10
pechora1.icann.org has A address 192.0.33.71
Priority 10
pechora4.icann.org has A address 192.0.33.74
Priority 10
pechora3.icann.org has A address 192.0.33.73


By trying this script against many different domains, you will be able to see how both big and small organizations
arrange for incoming e-mails to be routed to IP addresses.


Summary


Python programs often have to turn hostnames into socket addresses to which they can actually make connections.
Most hostname lookup should occur through the getsockaddr() function in the socket module, since its
intelligence is usually supplied by your operating system and it will know not only how to look up domain names
using all of the mechanisms available to it but also what flavor of address (IPv4 or IPv6) the local IP stack is configured
to support.
Traditional IPv4 addresses are still the most prevalent on the Internet, but IPv6 is becoming more and more
common. By deferring all hostname and port name lookup to getsockaddr(), your Python program can treat
addresses as opaque strings and not have to worry about parsing or interpreting them.
Behind most name resolution is the DNS, a worldwide-distributed database that forwards domain name queries
directly to the servers of the organization that owns a domain. While not often used directly from Python, it can be
helpful in determining where to direct e-mail based on the e-mail domain named after the @ sign in an e-mail address.
Now that you understand how to name the hosts to which you will then connect sockets, Chapter 5 will explore
the different options for encoding and delimiting the data payloads that you then transmit.

Free download pdf