Other answers may solve the problem, but there is a βmore twistedβ (and probably simpler) way to listen to the multicast group on multiple interfaces.
Listening to a group on one or more interfaces
To listen to a multicast group on one or more interfaces, specify the IP of each desired interface in several calls to the transport.joinGroup () method as the argument to "interface".
The following is an example that works on my Linux box and will force your system to listen to multicast messages on specific interfaces. Replace the IP addresses with those belonging to your system.
#!/usr/bin/env python from twisted.internet import protocol from twisted.internet import reactor class MyProtocol(protocol.DatagramProtocol): def startProtocol(self):
You can verify that the interface is listening on a new multicast group using the /sbin/ip maddr show . Locate the interface name you are looking for in the output of the command and verify that the multicast group is displayed below it.
An example UDP server associated with the original message should be able to do the same by changing the call to joinGroup () to include the second argument of the IP address, as indicated above.
Send multicast from a specific IP
If you receive multicast data on a socket, most likely you will also want to send multicast data - perhaps from several interfaces. Since it is closely related and there are very few examples, I will drop it here. Inside the twisted.internet.protocol.DatagramProtocol object, you can use the self.transport.setOutgoingInterface () method to control the source IP address, which will be used for subsequent calls to self.transport.write (). An example showing sending a message from multiple IP addresses / interfaces:
class MyProtocol(protocol.DatagramProtocol): # ... def send_stuff(self, msg): for src_ip in ["10.0.0.1", "192.168.1.1"]: self.transport.setOutgoingInterface(src_ip) self.transport.write(msg, ("224.0.0.9", 1520))
Suppose these IP addresses have been assigned to two different interfaces. Sniffing out Wireshark, you will see a message sent from the first interface, and then the second interface, using each IP as the source IP address for the corresponding transfer.