Chapter 2 ■ UDp
34
When setting socket options, you first have to name the option group in which they live and then, as a subsequent
argument, name the actual option you want to set. Consult your operating system manual for the names of these groups.
Just like the Python calls getattr() and setattr(), the set call simply takes one more argument than does the get.
value = s.getsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST)
s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, value)
Many options are specific to particular operating systems, and they may be finicky about how their options are
presented. Here are some of the more common options:
• SO_BROADCAST: This allows broadcast UDP packets to be sent and received, which I cover in
the next section.
• SO_DONTROUTE: Only be willing to send packets that are addressed to hosts on subnets to which
this computer is connected directly. My laptop, for example, at this moment would be willing
to send packets to the networks 127.0.0.0/8 and 192.168.5.0/24 if this socket option were set,
but it would not be willing to send them anywhere else because the packets would then have
to be routed through a gateway.
• SO_TYPE: When passed to getsockopt(), this returns to you whether a socket is of type
SOCK_DGRAM and can be used for UDP or whether it is of type SOCK_STREAM and instead
supports the semantics of TCP (see Chapter 3).
The next chapter will introduce some further socket options that apply specifically to TCP sockets.
Broadcast
If UDP has a superpower, it is its ability to support broadcast. Instead of sending a datagram to some other specific
host, you can address it to an entire subnet to which your machine is attached and have the physical network card
broadcast the datagram so that all attached hosts see it without its having to be copied separately to each one of them.
It should be immediately mentioned that broadcast is considered passé these days because a more sophisticated
technique called multicast has been developed that lets modern operating systems take better advantage of the
intelligence built into many networks and network interface devices. Also, multicast can work with hosts that are not
on the local subnet. But if you want an easy way to keep something such as gaming clients or automated scoreboards
up-to-date on the local LAN and each client can survive the occasional dropped packet, then UDP broadcast is an
easy choice.
Listing 2-4 shows an example of a server that can receive broadcast packets and a client that can send them.
If you look closely, you will see that there is pretty much just one difference between this listing and the techniques
used in previous listings. Before using this socket object, you call its setsockopt() method to turn on broadcast.
Aside from that, both server and client use the socket quite normally.
Listing 2-4. UDP Broadcast
#!/usr/bin/env python3
Foundations of Python Network Programming, Third Edition
https://github.com/brandon-rhodes/fopnp/blob/m/py3/chapter02/udp_broadcast.py
UDP client and server for broadcast messages on a local LAN
import argparse, socket
BUFSIZE = 65535