Chapter 4 ■ SoCket NameS aNd dNS
58
You will recall that socket names are important at several points in the creation and use of sockets. For your
reference, here are all of the major socket methods that demand of you some sort of socket name as an argument:
• mysocket.accept(): Each time this is called on a listening TCP stream socket that has
incoming connections ready to hand off to the application, it returns a tuple whose second
item is the remote address that has connected (the first item in the tuple is the new socket
connected to that remote address).
• mysocket.bind(address): This assigns the given local address to the socket so that outgoing
packets have an address from which to originate and so that any incoming connections from
other machines have a name to which they can connect.
• mysocket.connect(address): This establishes that data sent through this socket will be
directed to the given remote address. For UDP sockets, this simply sets the default address
used if the caller uses send() rather than sendto() or recv() instead of recvfrom() but does
not immediately perform any network communication. However, for TCP sockets, this actually
negotiates a new stream with another machine using a three-way handshake and raises a
Python exception if the negotiation fails.
• mysocket.getpeername(): This returns the remote address to which this socket is connected.
• mysocket.getsockname(): This returns the address of this socket’s own local endpoint.
• mysocket.recvfrom(...): For UDP sockets, this returns a tuple that pairs a string of returned
data with the address from which it was received.
• mysocket.sendto(data, address): An unconnected UDP port uses this method to fire off a
data packet at a particular remote address.
There you have it! Those are the major socket operations that care about socket addresses, all in one place, so that
you have some context for the remarks that follow. In general, any of the foregoing methods can receive or return any
of the sorts of addresses that follow, meaning they will work regardless of whether you are using IPv4, IPv6, or even
one of the less common address families that I will not be covering in this book.
Five Socket Coordinates
When studying the sample programs in Chapter 2 and Chapter 3, you paid particular attention to the hostnames and
IP addresses that their sockets used. But these are only the last two coordinates of five major decisions that were made
during the construction and deployment of each socket object. Recall that the steps go something like this:
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind(('localhost', 1060))
You can see that you specify four values here: two to configure the socket and two to address the bind() call.
There is actually a fifth possible coordinate because socket() takes a third, optional argument, making five choices in
all. I will discuss them each in turn, starting with the three possible parameters to socket().
First, the address family makes the biggest decision: it names what kind of network you want to talk to out of the
many kinds to which a particular machine might be connected.
In this book, I will always use the value AFINET for the address family because I believe that writing about IP
networking will best serve the vast majority of Python programmers while at the same time giving you skills that will
work on Linux, Mac OS, or even Windows. Nevertheless, if you import the socket module, print out dir(socket),
and look for the symbols that start with AF (“Address Family”), you will see other choices whose names you might