-
Notifications
You must be signed in to change notification settings - Fork 15
/
wifi_gate.py
49 lines (31 loc) · 1.02 KB
/
wifi_gate.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
import time
from contextlib import closing
from zeroconf import ServiceBrowser, Zeroconf
class MyListener(object):
wifi_gates = set()
def remove_service(self, zeroconf, type, name):
self.wifi_gates.remove(name)
def add_service(self, zeroconf, type, name):
self.wifi_gates.add(name)
def discover():
with closing(Zeroconf()) as zeroconf:
listener = MyListener()
listener.wifi_gates.clear()
browser = ServiceBrowser(zeroconf, "_jsongate._tcp.local.", listener)
time.sleep(1.0)
gates = [g.replace('._jsongate._tcp', '')[:-1]
for g in listener.wifi_gates]
return {
host.replace('.local', ''): (host, 9342)
for host in gates
}
def main():
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('cmd', choices=('discover', ))
args = parser.parse_args()
if args.cmd == 'discover':
while True:
print(discover())
if __name__ == '__main__':
main()