Foundations of Python Network Programming

(WallPaper) #1

Chapter 2 ■ UDp


22


So, my only option seemed to be a port from the reserved-but-not-well-known range above 1023. I glanced over
the list and made the gamble that you, gentle reader, are not running SAP BusinessObjects Polestar on the laptop or
desktop or server where you are running my Python scripts. If you are, then try giving the server a –p option to select a
different port number.
Note that the Python program can always use a socket’s getsockname() method to retrieve a tuple that contains
the current IP address and port to which the socket is bound.
Once the socket has been bound successfully, the server is ready to start receiving requests! It enters a loop and
repeatedly runs recvfrom(), telling the routine that it will happily receive messages up to a maximum length of 65,535
bytes—a value that happens to be the greatest length that a UDP datagram can possibly have, so that you will always
be shown the full content of each datagram. Until you send a message with a client, your recvfrom() call will wait
forever.
Once a datagram arrives, recvfrom() will return the address of the client that has sent you a datagram as well as
the datagram’s contents as bytes. Using Python’s ability to translate bytes directly to strings, you print the message to
the console and then return a reply datagram to the client.
So, let’s start up our client and examine the result. The client code is also shown in Listing 2-1.
(I hope, by the way, that it is not confusing that this example—like some of the others in the book—combines the
server and client code into a single listing, selected by command-line arguments. I often prefer this style since it keeps
server and client logic close to each other on the page, and it makes it easier to see which snippets of server code go
with which snippets of client code.)
While the server is still running, open another command window on your system, and try running the client
twice in a row like this:


$ python udp_local.py client
The OS assigned me the address ('0.0.0.0', 46056)
The server ('127.0.0.1', 1060) replied 'Your data was 46 bytes long'
$ python udp_local.py client
The OS assigned me the address ('0.0.0.0', 39288)
The server ('127.0.0.1', 1060) replied 'Your data was 46 bytes long'


Over in the server’s command window, you should see it reporting each connection that it serves.

The client at ('127.0.0.1', 46056) says 'The time is 2014-06-05 10:34:53.448338'
The client at ('127.0.0.1', 39288) says 'The time is 2014-06-05 10:34:54.065836'


Although the client code is slightly simpler than that of the server—there are only three lines of networking
code—it introduces two new concepts. The client call to sendto() provides both a message and a destination address.
This simple call is all that is necessary to send a datagram winging its way toward the server! But, of course, you need
an IP address and port number, on the client end, if you are going to be communicating. So, the operating system
assigns one automatically, as you can see from the output of the call to getsockname(). As promised, the client port
numbers are each from the IANA range for “ephemeral” port numbers. (At least they are here, on my laptop, under
Linux; under a different operating system, you might get a different result.)
When you are done with the server, you can kill it by pressing Ctrl+C in the terminal where it is running.

Free download pdf