Foundations of Python Network Programming

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

59

recognize, like AppleTalk and Bluetooth. Especially popular on POSIX systems is the AF_UNIX address family, which
offers connections very much like Internet sockets but that run directly between programs on the same machine by
“connecting” to filenames instead of hostnames and port numbers.
Second, after the address family comes the socket type. It chooses the particular kind of communication
technique that you want to use on the network you have chosen. You might guess that every single address family
presents entirely different socket types that you would have to go and look up for each one. After all, what address
family besides AF_INET is going to present socket types like UDP and TCP?
Happily, this suspicion is misplaced. Although UDP and TCP are indeed quite specific to the AF_INET protocol
family, the socket interface designers decided to create more generic names for the broad idea of a packet-based
socket. This goes by the name SOCK_DGRAM, and the broad idea of a reliable flow-controlled data stream, which, as you
have seen, is known as a SOCK_STREAM. Because many address families support either one or both of these kinds of
mechanisms, only these two symbols are necessary to cover many protocols under a variety of different address families.
The third field in the socket() call, the protocol, is rarely used because once you have specified the address
family and socket type, you have usually narrowed down the possible protocols to only one major option. Thus,
programmers usually leave this unspecified, or they provide the value 0 to force it to be chosen automatically. If you
want a stream under IP, the system knows to choose TCP. If you want datagrams, then it selects UDP. That is why none
of the socket() calls in this book has a third argument: it is almost never needed in practice. Look inside the socket
module for names starting with IPPROTO for some examples of protocols defined for the AF_INET family. Listed there
you will see the two this book actually addresses, under the names IPPROTO_TCP and IPPROTO_UDP.
Finally, the fourth and fifth values used to make a connection are the IP address and port number that were
explained in detail in the previous two chapters.
We should immediately step back and note that it is only because of our specific choices for the first three
coordinates that our socket names have had two components: hostname and port. If you instead had chosen
AppleTalk or ATM or Bluetooth for your address family, then some other data structure might have been required
instead of a tuple with a string and an integer inside. So, the whole set of coordinates, which I have talked about as five
coordinates in this section, is really the three fixed coordinates needed to create the socket, followed by however many
more coordinates your particular address family requires you to use in order to make a network connection.


IPv6

Now, having explained all of that, it turns out that this book actually does need to introduce one additional address
family beyond the AF_INET used so far: the address family for IPv6, named AF_INET6, which is the way forward into a
future where the world does not ultimately run out of IP addresses.
Once the old ARPANET really started taking off, its choice of 32-bit address names—which made so much sense
back when computer memory was measured by the kilobyte—became a clear and worrying limitation. Only 4 billion
possible addresses available provides less than one IP address for every person on the earth, and that means real
trouble once everyone has both a computer and a smartphone!
Even though only a small percentage of the computers on the Internet today are actually using IPv6 to
communicate with the global network through their Internet service providers (where “today” is June 2014), the steps
necessary to make your Python programs compatible with IPv6 are simple enough so that you should go ahead and
try writing code that prepares you for the future.
In Python, you can test directly for whether the underlying platform supports IPv6 by checking the has_ipv6
Boolean attribute inside the socket module.





import socket
socket.has_ipv6
True





Note that this does not tell you whether an actual IPv6 interface is up and configured and can currently be used
to send packets anywhere! It is purely an assertion about whether IPv6 support has been compiled into the operating
system, not about whether it is in use.

Free download pdf