-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzero-server.py
More file actions
executable file
·61 lines (47 loc) · 1.85 KB
/
Copy pathzero-server.py
File metadata and controls
executable file
·61 lines (47 loc) · 1.85 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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env python
"""Loop indefinitely, announcing the service via zeroconf.
Use this to test clients.
call it like so:
python ./zero-server.py 192.168.1.99 3000
Just use the IP and port your server is advertising.
Even more data can be embedded in the properties of the ServiceInfo object.
"""
import argparse
import logging
import socket
from time import sleep
from zeroconf import ServiceInfo, Zeroconf
def main(parsed_args):
"""Advertise a service on the local subnet using mDNS."""
# set the log level based on the verbosity they need.
logging.basicConfig(level=logging.INFO)
if parsed_args.verbose:
logging.getLogger('zeroconf').setLevel(logging.DEBUG)
portnum = int(parsed_args.port)
info = ServiceInfo(type_="_http._tcp.local.",
name="pcvolumecontrol._http._tcp.local.",
address=socket.inet_aton(parsed_args.ip),
port=portnum,
weight=0,
priority=0,
properties={'missiles': 'armed'},
server="myservername.local.")
zeroconf = Zeroconf()
print("Service is now registered at: {}:{}, press Ctrl-C to exit...".format(parsed_args.ip, portnum))
zeroconf.register_service(info)
try:
while True:
sleep(0.1)
except KeyboardInterrupt:
pass
finally:
print("Unregistering...")
zeroconf.unregister_service(info)
zeroconf.close()
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Advertise a service on the LAN using mDNS.')
parser.add_argument('ip', action='store', help='IP address of the service')
parser.add_argument('port', action='store', help='Port of the service')
parser.add_argument('--verbose', '-v', action='store_true')
args = parser.parse_args()
main(args)