23 lines
779 B
Python
23 lines
779 B
Python
|
|
import socket
|
|
|
|
MCAST_GRP = '239.255.255.255'
|
|
MCAST_PORT = 5007
|
|
# regarding socket.IP_MULTICAST_TTL
|
|
# ---------------------------------
|
|
# for all packets sent, after two hops on the network the packet will not
|
|
# be re-sent/broadcast (see https://www.tldp.org/HOWTO/Multicast-HOWTO-6.html)
|
|
MULTICAST_TTL = 2
|
|
|
|
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
|
|
sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, MULTICAST_TTL)
|
|
|
|
# For Python 3, change next line to 'sock.sendto(b"robot", ...' to avoid the
|
|
# "bytes-like object is required" msg (https://stackoverflow.com/a/42612820)
|
|
|
|
from datetime import datetime
|
|
import time
|
|
for i in range(100):
|
|
dsend = str(i)+' '+str(datetime.now())
|
|
sock.sendto(dsend.encode(), (MCAST_GRP, MCAST_PORT))
|