-
Notifications
You must be signed in to change notification settings - Fork 50
/
coapreverseproxy.py
52 lines (40 loc) · 1.36 KB
/
coapreverseproxy.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
#!/usr/bin/env python
import getopt
import sys
from coapthon.reverse_proxy.coap import CoAP
__author__ = 'Giacomo Tanganelli'
class CoAPReverseProxy(CoAP):
def __init__(self, host, port, xml_file, multicast=False, cache=False, starting_mid=None):
CoAP.__init__(self, (host, port), xml_file=xml_file, multicast=multicast, starting_mid=starting_mid,
cache=cache)
print("CoAP Proxy start on " + host + ":" + str(port))
def usage(): # pragma: no cover
print("coapreverseproxy.py -i <ip address> -p <port> -f <xml_file>")
def main(argv): # pragma: no cover
ip = "0.0.0.0"
port = 5684
file_xml = "reverse_proxy_mapping.xml"
try:
opts, args = getopt.getopt(argv, "hi:p:f:", ["ip=", "port=", "file="])
except getopt.GetoptError:
usage()
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
usage()
sys.exit()
elif opt in ("-i", "--ip"):
ip = arg
elif opt in ("-p", "--port"):
port = int(arg)
elif opt in ("-f", "--file"):
file_xml = arg
server = CoAPReverseProxy(ip, port, file_xml)
try:
server.listen(10)
except KeyboardInterrupt:
print("Server Shutdown")
server.close()
print("Exiting...")
if __name__ == "__main__": # pragma: no cover
main(sys.argv[1:])