[Python编程(第4版)].(Programming.Python.4th.Edition).Mark.Lutz.文字版

(yzsuai) #1

Server socket calls


Before we see these programs in action, let’s take a minute to explain how this client
and server do their stuff. Both are fairly simple examples of socket scripts, but they
illustrate the common call patterns of most socket-based programs. In fact, this is boil-
erplate code: most connected socket programs generally make the same socket calls
that our two scripts do, so let’s step through the important points of these scripts line
by line.


Programs such as Example 12-1 that provide services for other programs with sockets
generally start out by following this sequence of calls:


sockobj = socket(AF_INET, SOCK_STREAM)
Uses the Python socket module to create a TCP socket object. The names
AF_INET and SOCK_STREAM are preassigned variables defined by and imported from
the socket module; using them in combination means “create a TCP/IP socket,”
the standard communication device for the Internet. More specifically, AF_INET
means the IP address protocol, and SOCK_STREAM means the TCP transfer protocol.
The AF_INET/SOCK_STREAM combination is the default because it is so common, but
it’s typical to make this explicit.
If you use other names in this call, you can instead create things like UDP connec-
tionless sockets (use SOCK_DGRAM second) and Unix domain sockets on the local
machine (use AF_UNIX first), but we won’t do so in this book. See the Python library
manual for details on these and other socket module options. Using other socket
types is mostly a matter of using different forms of boilerplate code.


sockobj.bind((myHost, myPort))
Associates the socket object with an address—for IP addresses, we pass a server
machine name and port number on that machine. This is where the server identifies
the machine and port associated with the socket. In server programs, the hostname
is typically an empty string (“”), which means the machine that the script runs on
(formally, all available local and remote interfaces on the machine), and the port
is a number outside the range 0 to 1023 (which is reserved for standard protocols,
described earlier).
Note that each unique socket dialog you support must have its own port number;
if you try to open a socket on a port already in use, Python will raise an exception.
Also notice the nested parentheses in this call—for the AF_INET address protocol
socket here, we pass the host/port socket address to bind as a two-item tuple object
(pass a string for AF_UNIX). Technically, bind takes a tuple of values appropriate for
the type of socket created.


sockobj.listen(5)
Starts listening for incoming client connections and allows for a backlog of up to
five pending requests. The value passed sets the number of incoming client requests
queued by the operating system before new requests are denied (which happens
only if a server isn’t fast enough to process requests before the queues fill up). A


790 | Chapter 12: Network Scripting

Free download pdf