def localnet_register(host, port):
'''
Runs a never-exiting thread which only registers a local network service
via Zeroconf and then responds to info requests.
'''
try:
from zeroconf import ServiceInfo, Zeroconf
from time import sleep
except ImportError as e:
logging.error(
'Zeroconf not installed, cannot register this server on the local '
'network. Other players may still connect, but they must be told '
'what your hostname and port are (hostname: {}, port: {})'.format(
host, port))
return
advertised_interface = local_address('127.0.0.1')
info = ServiceInfo(
"_defusedivision._tcp.local.",
"{}{}._defusedivision._tcp.local.".format(
host.replace('.', '-'), advertised_interface.replace('.', '-')),
address=socket.inet_aton(advertised_interface),
port=int(port),
weight=0,
priority=0,
properties=b"")
zc = Zeroconf()
zc.register_service(info)
atexit.register(lambda: zc.close())
while True:
sleep(0.1)
评论列表
文章目录