-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathtwisted_discovery.py
More file actions
executable file
·33 lines (27 loc) · 1.03 KB
/
twisted_discovery.py
File metadata and controls
executable file
·33 lines (27 loc) · 1.03 KB
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
#!/usr/bin/env python
from twisted.internet.protocol import DatagramProtocol
from twisted.internet import reactor
import struct
import platform
from pyrf.devices.thinkrf import (DISCOVERY_UDP_PORT, DISCOVERY_QUERY,
parse_discovery_response)
from pyrf.windows_util import get_broadcast_addresses
WAIT_TIME = 0.125
class DiscoverWSAs(DatagramProtocol):
def startProtocol(self):
import socket
self.transport.socket.setsockopt(
socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
if platform.system() == 'Windows':
destinations = get_broadcast_addresses()
else:
destinations = ['<broadcast>']
for d in destinations:
self.transport.socket.sendto(DISCOVERY_QUERY,
(d, DISCOVERY_UDP_PORT))
def datagramReceived(self, data, (host, port)):
model, serial, firmware = parse_discovery_response(data)
print model, serial, firmware, 'at', host
reactor.listenUDP(0, DiscoverWSAs())
reactor.callLater(WAIT_TIME, reactor.stop)
reactor.run()