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 />
But we should immediately step back, and note that it is only because <strong>of</strong> our specific choices for the<br />
first three coordinates that our socket names have had two components, hostname and port! If you<br />
instead had chosen AppleTalk or ATM or Bluetooth for your address family, then some other data<br />
structure might have been required <strong>of</strong> you instead <strong>of</strong> a tuple with a string and an integer inside. So the<br />
whole set <strong>of</strong> coordinates, which in this section we have talked about as five coordinates, is really three<br />
fixed coordinates needed to create the socket, followed by however many more coordinates your<br />
particular address family requires you to use in order to make a network connection.<br />
IPv6<br />
And having explained all <strong>of</strong> that, it turns out that this book actually does need to introduce one<br />
additional address family, beyond the AF_INET we have used so far: the address family for IPv6, named<br />
AF_INET6, which is the way forward into a future where the world does not, in fact, run out <strong>of</strong> IP<br />
addresses.<br />
Once the old ARPANET really started taking <strong>of</strong>f, its choice <strong>of</strong> 32-bit address names—which made so<br />
much sense back when computer memory chips were measured by the kilobyte—became a clear and<br />
worrying limitation. With only about four billion possible addresses available, even assuming that we<br />
could use the address space that fully, that makes fewer than one IP address for every person on the<br />
earth—which means real trouble once everyone has both a computer and an iPhone!<br />
Even though only a few percent <strong>of</strong> the computers on the Internet today are actually using IPv6 to<br />
communicate with the global network through their Internet service providers (where “today” is<br />
September 2010), the steps necessary to make your <strong>Python</strong> programs compatible with IPv6 are simple<br />
enough that you should go ahead and try writing code that prepares us all for the future.<br />
In <strong>Python</strong> you can test directly for whether the underlying platform supports IPv6 by checking the<br />
has_ipv6 Boolean attribute inside the socket module:<br />
>>> import socket<br />
>>> socket.has_ipv6<br />
True<br />
But note that this does not tell you whether an actual IPv6 interface is up and configured and can<br />
currently be used to send packets anywhere; it is purely an assertion about whether IPv6 support has<br />
been compiled into the operating system, not about whether it is in use!<br />
The differences that IPv6 will make for your <strong>Python</strong> code might sound quite daunting, if listed one<br />
right after the other:<br />
• Your sockets have to be prepared to have the family AF_INET6 if you are called<br />
upon to operate on an IPv6 network.<br />
• No longer do socket names consist <strong>of</strong> just two pieces, an address and a port<br />
number; instead, they can also involve additional coordinates that provide “flow”<br />
information and a “scope” identifier.<br />
• The pretty IPv4 octets like 18.9.22.69 that you might already be reading from<br />
configuration files or from your command-line arguments will now sometimes be<br />
replaced by IPv6 host addresses instead, which you might not even have good<br />
regular expressions for yet. They have lots <strong>of</strong> colons, they can involve hexadecimal<br />
numbers, and in general they look quite ugly.<br />
The benefits <strong>of</strong> the IPv6 transition are not only that it will make an astronomically large number <strong>of</strong><br />
addresses available, but also that the protocol has more complete support for things like link-level<br />
security than do most implementations <strong>of</strong> IPv4.<br />
But the changes just listed can sound like a lot <strong>of</strong> trouble if you have been in the habit <strong>of</strong> writing<br />
clunky, old-fashioned code that puts IP addresses and hostnames through regular expressions <strong>of</strong> your<br />
54