Foundations of Python Network Programming

(WallPaper) #1
Chapter 2 ■ UDp

19

•    Manual configuration: For all of the situations that are not covered by the previous two cases,
manual intervention by an administrator or user will have to deliver an IP address or the
corresponding hostname of a service. Manual configuration in this sense is happening, for
example, every time you type a web server name into your web browser.

When making decisions about defining port numbers, such as 53 for DNS, IANA thinks of them as falling into
three ranges—and this applies to both UDP and TCP port numbers.


•    Well-known ports (0–1023) are for the most important and widely used services. On many
Unix-like operating systems, normal user programs cannot listen on these ports. In the old
days, this prevented troublesome undergraduates on multiuser university machines from
running programs that masqueraded as important system services. Today the same caution
applies when hosting companies hand out command-line Linux accounts.

•    Registered ports (1024–49151) are not usually treated as special by operating systems—any
user can write a program that grabs port 5432 and pretends to be a PostgreSQL database, for
example—but they can be registered by IANA for specific services, and IANA recommends you
avoid using them for anything but their assigned service.

•    The remaining port numbers (49152–65535) are free for any use. They, as you will see, are the
pool on which modern operating systems draw in order to generate arbitrary port numbers
when a client does not care what port it is assigned for its outgoing connection.

When you craft programs that accept port numbers from user input such as the command line or configuration
files, it is friendly to allow not just numeric port numbers but human-readable names for well-known ports. These
names are standard, and they are available through the getservbyname() function inside Python’s standard socket
module. If you want to ask the port for the Domain Name Service, you can find out this way:





import socket
socket.getservbyname('domain')
53





As you will see in Chapter 4, port names can also be decoded by the more complicated getaddrinfo() function,
which is also provided by the socket module.
The database of well-known service names and port numbers is usually kept in the file /etc/services on Linux
and Mac OS X machines, which you can peruse at your leisure. The first few pages of the file, in particular, are littered
with ancient protocols that still have reserved numbers despite not having had an actual packet addressed to them
anywhere in the world for many years. An up-to-date (and typically much more extensive) copy is also maintained
online by IANA at http://www.iana.org/assignments/port-numbers.


Sockets


Rather than trying to invent its own API for network programming, Python made an interesting decision. At bottom,
Python’s Standard Library simply provides an object-based interface to all of the normal, gritty, low-level operating
system calls that are normally used to accomplish networking tasks on POSIX-compliant operating systems. The calls
even have the same names as the underlying operations they wrap. Python’s willingness to expose the traditional
system calls that everyone already understood before it came on the scene is one of the reasons that Python came
as such a breath of fresh air to those of us toiling in lower-level languages in the early 1990s. Finally, a higher-level
language had arrived that let us make low-level operating system calls when we needed them, without insisting that
we use an awkward, underpowered but ostensibly “prettier” language-specific API instead. It was much easier to
remember a single set of calls that worked in both C and Python.

Free download pdf