'python multicast: how to disable IP_MULTICAST_LOOP

Trying this code:

import socket
mcast_group = "239.255.0.1"
mcast_port = 4444
sock_rx = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock_rx.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock_rx.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_LOOP, b'\x00')
sock_rx.bind((mcast_group, mcast_port))
mreq = bytes((239, 255, 0, 1, 0, 0, 0, 0))
sock_rx.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)

sock_tx = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

sock_tx.sendto(b'hello', (mcast_group, mcast_port))
sock_rx.recvfrom(1024)

sock_rx.recvfrom() is still receiving messages sent from sock_tx.

although is disabled with socket.IP_MULTICAST_LOOP writing 0.

I tried this code on Linux and MACOS with same result.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source