Created
April 21, 2017 13:38
-
-
Save paretech/66afc68f075c925a32db6ba210f7a170 to your computer and use it in GitHub Desktop.
A simple multicast server that repeatedly says "Test"
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
"""multicast_server.py - A simple multicast server that says "Test".""" | |
import socket | |
import struct | |
import time | |
class multicast_speaker(): | |
def __init__(self, address, port, ttl=2, timeout=0.2): | |
self.group = address, int(port) | |
self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | |
ttl_bin = struct.pack('@i', ttl) | |
self.sock.settimeout(timeout) | |
self.sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, ttl_bin) | |
def send(self, value): | |
self.sock.sendto(value, self.group) | |
if __name__ == '__main__': | |
# group = ('225.0.0.1', 2001) | |
sock = multicast_speaker('225.0.0.1', '2101') | |
while True: | |
try: | |
sock.send(b'Test\n') | |
print('Test') | |
except OSError as SocketError: | |
print(SocketError) | |
time.sleep(.1) | |
# Exceptions | |
# 1. OSError: [Errno 50] Network is down | |
# 2. OSError: [Errno 55] No buffer space available |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment