forked from zdavkeos/addp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
discover.py
52 lines (38 loc) · 1.08 KB
/
discover.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/usr/bin/env python
"""
Discover ADDP devices on the local network
"""
import sys
import socket
from pprint import pprint
from addp import build_request, parse_frame
def send_discovery():
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 2)
# you can only use the REUSE options OR the bind, not both
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
try:
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
except AttributeError:
pass
sock.bind(("", 2362))
sock.settimeout(1.0)
msg = build_request(0x01, mac=(255,255,255,255,255,255))
sock.sendto(msg, ("224.0.5.128", 2362))
responses = []
while True:
try:
data, addr = sock.recvfrom(2048)
except:
break
if data is None or data == '':
break
info = parse_frame(data)
if info:
info['addp_ip'] = addr[0]
responses.append(info)
sock.close()
return responses
if __name__ == '__main__':
sys.stderr.write('Sending discover message...\n')
pprint(send_discovery())