Choosing a Multicast Network Interface in Python

I have a server with two separate Ethernet connections. When I bind a socket in python, it defaults to one of two networks. How to migrate a multicast stream from a second network in Python? I tried calling bind using the IP address of the server on the second network, but this did not work.

+6
python networking sockets
source share
3 answers

I get it. It turns out that the part I was missing was adding an interface to the mreq structure, which is used when adding membership to a multicast group.

0
source share

I recommend that you do not use INADDR_ANY. In production multicast environments, you want to be very specific on your multicast sockets and don't want to do things like sending igmp integrates all the interfaces. This leads to workarounds for hackers when everything does not work like "route add -host 239.1.1.1 dev eth3" to receive multicast connections in the correct order depending on the system in question. Use this instead:

def joinMcast(mcast_addr,port,if_ip): """ Returns a live multicast socket mcast_addr is a dotted string format of the multicast group port is an integer of the UDP port you want to receive if_ip is a dotted string format of the interface you will use """ #create a UDP socket mcastsock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM) #allow other sockets to bind this port too mcastsock.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1) #explicitly join the multicast group on the interface specified mcastsock.setsockopt(socket.SOL_IP,socket.IP_ADD_MEMBERSHIP, socket.inet_aton(mcast_addr)+socket.inet_aton(if_ip)) #finally bind the socket to start getting data into your socket mcastsock.bind((mcast_addr,port)) return mcastsock 

In mcastsock.bind you can also use '' instead of the address bar, but I advise against this. With ``, if you have a different socket using the same port, both sockets will receive data from each other.

+12
source share

When bind enter your socket, try the values here :

For IPv4 addresses, two special forms are accepted instead of the host address: the empty string represents INADDR_ANY and the string '' represents INADDR_BROADCAST.

INADDR_ANY also known as a wildcard address:

Sockets with wildcard local addresses can receive messages directed to the specified port number and addressed to any of the possible addresses assigned to the host

More details here .

+1
source share

All Articles