-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass_server.py
67 lines (52 loc) · 1.73 KB
/
class_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
65
66
67
#!/usr/bin/env python
from __future__ import print_function # Print function in Python 2 and 3
import argparse
import sys
from coapthon.server.coap import CoAP
from class_resources import BasicResource, ObservableResource, CPUResource, MemResource, PsutilResource
"""
Server CoAP for observe tracker
"""
__author__ = 'Fco R Tendero'
def ignore_listen_exception(exception, server):
type = exception.__class__.__name__
print(type)
return True
def main(args):
# IP and Port configuration
ip = "0.0.0.0"
port = 5683
# print which type of transmission is used:
if args.unicast:
print("Unicast transmission")
else:
print("Multicast transmission")
# Create the CoAP server
server = CoAP(
(ip, port),
multicast=not args.unicast,
cb_ignore_listen_exception=ignore_listen_exception
)
# Register resources
server.add_resource('info/', BasicResource(coap_server=server))
server.add_resource('time/', ObservableResource(coap_server=server))
server.add_resource('cpu/', CPUResource(coap_server=server))
server.add_resource('memory/', MemResource(coap_server=server))
server.add_resource('psutil/', PsutilResource(coap_server=server))
try:
# Start listening for incoming requests
server.listen()
except KeyboardInterrupt:
print("Server Shutdown")
server.close()
print("Exiting...")
sys.exit(2)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
'-U', '--unicast',
help="If flag is used: unicast transmission. If not (default): multicast transmission",
default=False,
action='store_true'
)
main(parser.parse_args())