-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsd_server.py
executable file
·64 lines (53 loc) · 2.4 KB
/
sd_server.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env python3
import logging
import argparse
import asyncio
import ipaddress
from soa.directory import ServiceDirectory
from soa.directory import coap
from soa.directory import http
loglevels = [logging.CRITICAL, logging.ERROR, logging.WARN, logging.INFO, logging.DEBUG]
def main(argv=None):
parser = argparse.ArgumentParser(argv)
parser.add_argument('-f', '--dbfile', type=str, metavar='FILE', default='directory_db',
help="Service directory database filename")
parser.add_argument("-v", "--verbosity", type=int, default=4,
help="set logging verbosity, 1=CRITICAL, 5=DEBUG")
parser.add_argument("--coap-port", type=int, default=5683,
help="CoAP port, use 0 to disable")
parser.add_argument("--coap-bind", default='::',
help="CoAP server bind address")
parser.add_argument("--http-port", type=int, default=8045,
help="HTTP port, use 0 to disable")
parser.add_argument("--http-bind", default='::',
help="HTTP server bind address")
args = parser.parse_args()
# logging setup
logging.basicConfig(level=loglevels[args.verbosity-1])
# selective log levels
#logging.getLogger("coap-server").setLevel(logging.DEBUG)
#logging.getLogger("soa").setLevel(logging.DEBUG)
directory = ServiceDirectory(args.dbfile)
loop = asyncio.get_event_loop()
if args.coap_port:
# aiocoap only supports IPv6 sockets, use ::ffff:123.45.67.89 for
# listening on IPv4 addresses
coap_addr = ipaddress.ip_address(args.coap_bind)
if isinstance(coap_addr, ipaddress.IPv4Address):
coap_bind = '::ffff:' + str(coap_addr)
else:
coap_bind = str(coap_addr)
# TODO: Update this when aiocoap 0.3 is released on PyPi (loop support)
#coap_server = coap.Server(directory=directory, loop=loop)
coap_server = coap.Server(directory=directory, bind=(coap_bind, args.coap_port))
asyncio.async(coap_server.context)
if args.http_port:
http_directory = http.Server(directory=directory, loop=loop)
http_handler = http_directory.make_handler()
http_server = loop.create_server(http_handler, host=args.http_bind,
port=args.http_port)
asyncio.async(http_server)
loop.run_forever()
if __name__ == "__main__":
import sys
main(sys.argv)