-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzc.py
33 lines (29 loc) · 939 Bytes
/
zc.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
import zeroconf
zc = None
def get_lanip():
import socket
ipaddrlist = socket.gethostbyname_ex(socket.gethostname())[2]
if len(ipaddrlist) == 0 or ipaddrlist[-1] == '127.0.0.1':
return None
return ipaddrlist[-1]
def add_zeroconf_service(hostname, port):
global zc
# print('Registering zeroconf service...')
lanip = get_lanip()
if lanip == None:
print('Zeroconf couldn\'t get local LAN IP')
return False
service_info = zeroconf.ServiceInfo(
"_ws._tcp.local.",
f'{hostname}._ws._tcp.local.',
addresses=[lanip],
port=port,
server=f"{hostname}.local."
)
zc = zeroconf.Zeroconf()
zc.register_service(service_info)
print(f'Zeroconf: Registered {hostname}.local -> {lanip} (Port {port})')
def remove_zeroconf_service():
if zc != None:
print('Unregistering zeroconf service...')
zc.unregister_all_services()