MCPcopy Create free account
hub / github.com/ActiveState/code / Beacon

Class Beacon

recipes/Python/577950_Socket_Broadcast_Help/recipe-577950.py:20–56  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

18################################################################################
19
20class Beacon:
21
22 __slots__ = '__sock', '__addr'
23
24 def __init__(self, port):
25 "Initialize the beacon for sending and receiving data."
26 self.__sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
27 self.__sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, True)
28 self.__sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, True)
29 self.__sock.bind(('0.0.0.0', port))
30 self.__addr = '255.255.255.255', port
31
32 def __del__(self):
33 "Shutdown and close the underlying socket."
34 self.__sock.shutdown(socket.SHUT_RDWR)
35 self.__sock.close()
36
37 def recv(self, size):
38 "Receive a broadcast through the underlying socket."
39 return self.__sock.recvfrom(size)
40
41 def send(self, data):
42 "Send a broadcast through the underlying socket."
43 assert self.__sock.sendto(data, self.__addr) == len(data), \
44 'Not all data was sent through the socket!'
45
46 def __gettimeout(self):
47 return self.__sock.gettimeout()
48
49 def __settimeout(self, value):
50 self.__sock.settimeout(value)
51
52 def __deltimeout(self):
53 self.__sock.setblocking(True)
54
55 timeout = property(__gettimeout, __settimeout, __deltimeout,
56 'Timeout on blocking socket operations.')
57
58################################################################################
59

Callers 1

testFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected