UDP broadcast over a specific interface

Jean-Paul Calderone exarkun at divmod.com
Thu Jul 19 15:09:08 CEST 2007

On Thu, 19 Jul 2007 12:32:02 -0000, "Larry.Martell at gmail.com" <larry.martell at gmail.com> wrote:
>I am trying to send UDP broadcast packets over a specific interface
>and I
>am having trouble specifying the interface:
>sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
>sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
>sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_IF,
>socket.error: (49, "Can't assign requested address")
>What am I doing wrong? How can I force my broadcast packets to go out
>a specific interface?

IP_MULTICAST_IF is for multicast UDP, which doesn't have anything to do
with broadcast UDP.

Try just doing this, instead:

    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)

You shouldn't need to mess with anything beyond that.


More information about the Python-list mailing list