CoAP/coapreverseproxy.py

53 lines
1.4 KiB
Python
Raw Normal View History

2018-01-23 11:31:46 +01:00
#!/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)
2019-04-23 11:37:18 +02:00
print("CoAP Proxy start on " + host + ":" + str(port))
2018-01-23 11:31:46 +01:00
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:])