Foundations of Python Network Programming

(WallPaper) #1

Chapter 4 ■ SoCket NameS aNd dNS


60


The differences that IPv6 will make for your Python code might sound quite daunting if listed one right after
the other.


•    Your sockets have to be created with the family AF_INET6 if you are called upon to operate on
an IPv6 network.

•    No longer do socket names consist of just two pieces—an address and a port number. Instead,
they can also involve additional coordinates that provide “flow” information and a “scope”
identifier.

•    The pretty IPv4 octets like 18.9.22.69 that you might already be reading from configuration
files or from your command-line arguments will now sometimes be replaced by IPv6 host
addresses instead, and you might not even have good regular expressions for these just yet.
They have lots of colons, they can involve hexadecimal numbers, and in general they look
quite ugly.

The benefits of the IPv6 transition are not only that it will make an astronomically large number of addresses
available but also that the protocol has more complete support for things such as link-level security than do most
implementations of IPv4.
But the changes just listed can sound like a lot of trouble if you are in the habit of writing clunky, old-fashioned code
that scans or assembles IP addresses and hostnames through regular expressions of your own devising. In other words, if
you have been in the business of interpreting addresses yourself in any form, you probably imagine that the transition to
IPv6 will make you write even more complicated code than previously. Fear not: my actual recommendation is that you
get out of address interpretation and scanning altogether! The next section will show you how.


Modern Address Resolution


To make your code simple, powerful, and immune from the complexities of the transition from IPv4 to IPv6, you
should turn your attention to one of the most powerful tools in the Python socket user’s arsenal: getaddrinfo().
The getaddrinfo() function sits in the socket module along with most other operations that involve addresses.
Unless you are doing something specialized, it is probably the only routine that you will ever need to use to transform
the hostnames and port numbers that your users specify into addresses that can be used by socket methods.
Its approach is simple. Rather than making you attack the addressing problem piecemeal, which is necessary
when using the older routines in the socket module, it lets you specify everything you know about the connection
that you need to make in a single call. In response, it returns all of the coordinates that I discussed earlier, which are
necessary for you to create and connect a socket to the named destination.
Its basic use is simple, and it goes like this (note that the pprint “pretty print” module has nothing to do with
networking, but it will simply do a better job of displaying a list of tuples than the normal print function):





from pprint import pprint
infolist = socket.getaddrinfo('gatech.edu', 'www')
pprint(infolist)
[(2, 1, 6, '', ('130.207.244.244', 80)),
(2, 2, 17, '', ('130.207.244.244', 80))]
info = infolist[0]
info[0:3]
(2, 1, 6)
s = socket.socket(*info[0:3])
info[4]
('130.207.244.244', 80)
s.connect(info[4])




Free download pdf