Created
July 29, 2023 08:22
-
-
Save wention/168b20be38eaed223b7a13fbe08ccf2f to your computer and use it in GitHub Desktop.
multicast echo example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import socket | |
import struct | |
def multicast_receive(multicast_group, port): | |
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | |
sock.bind(('', port)) | |
group = socket.inet_aton(multicast_group) | |
mreq = struct.pack('4sL', group, socket.INADDR_ANY) | |
sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq) | |
return sock | |
def main(): | |
addr, port = sys.argv[1].split(':') | |
sock = multicast_receive(addr, int(port)) | |
while True: | |
print("waiting to receive message") | |
data, address = sock.recvfrom(1024) | |
print('received %s bytes from %s' % (len(data), address)) | |
if __name__ == '__main__': | |
if len(sys.argv) < 2: | |
print("Usage: %s address:port" % sys.argv[0]) | |
sys.exit(-1) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment