Foundations of Python Network Programming

(WallPaper) #1

Chapter 2 ■ UDp


36


But when you use the local network’s broadcast address, suddenly you will see that all of the broadcast servers get
the packet at the same time! (But no normal servers will see it—run a few copies of the normal udp_remote.py server
while making broadcasts to be convinced.) On my local network at the moment, the ifconfig command tells me that
the broadcast address is this:


$ python udp_broadcast.py client 192.168.5.255


And, sure enough, both servers immediately report that they see the message. In case your operating system
makes it difficult to determine the broadcast address and you do not mind doing a broadcast out of every single
network port of your host, Python lets you use the special hostname '' when sending with a UDP socket.
Be careful to quote that name when passing it to your client, since the < and > characters are quite special to any
normal POSIX shell.


$ python udp_broadcast.py client ""


If there were any platform-independent way to learn each connected subnet and its broadcast address, I would
show you. Unfortunately, you will have to consult your own operating system documentation if you want to do
anything more specific than use this special '' string.


When to Use UDP

You might think that UDP would be efficient for sending small messages. Actually, UDP is efficient only if your host
sends only one message at a time and then waits for a response. If your application might send several messages in a
burst, then using an intelligent message queue like ØMQ will actually be more efficient because it will set a short timer
that lets it bundle several small messages together into a single transmission, probably on a TCP connection that does
a much better job of splitting the payload into fragments than you would!
There are, however, a few good reasons to use UDP.


•    Because you are implementing a protocol that already exists and it uses UDP.

•    Because you are designing a time-critical media stream whose redundancy allows for
occasional packet loss and you never want this second’s data getting hung up waiting for old
data from several seconds ago that has not yet been delivered (as happens with TCP).

•    Because unreliable LAN subnet multicast is a great pattern for your application and UDP
supports it perfectly.

Outside of these three situations, you should probably look at the latter chapters of this book for inspiration about
how to construct the communication for your application. There is an old saying that by the time you have a UDP
protocol kind of working for your application, you have probably just reinvented TCP—badly.


Summary


The User Datagram Protocol lets user-level programs send individual packets across an IP network. Typically, a client
program sends a packet to a server, which then replies using the return address built into every UDP packet.
The POSIX network stack gives you access to UDP through the idea of a “socket,” which is a communications
endpoint that can sit at an IP address and UDP port number—these two things together are called the socket’s name
or address—and send and receive datagrams. Python offers these primitive network operations through the built-in
socket module.

Free download pdf