tools: support IPv6 in nfd-status-http-server
Change-Id: I40866b0caf25315123f0da5a3136b576ece19027
Refs: #1445
diff --git a/tools/nfd-status-http-server.py b/tools/nfd-status-http-server.py
index 5221399..ccfaee5 100755
--- a/tools/nfd-status-http-server.py
+++ b/tools/nfd-status-http-server.py
@@ -9,6 +9,7 @@
import logging
import cgi
import argparse
+import socket
class StatusHandler(BaseHTTPRequestHandler):
@@ -46,20 +47,10 @@
self.end_headers()
self.wfile.write(resultMessage)
- def do_HEAD(self):
- self.send_response(405)
- self.send_header("Content-type", "text/plain")
- self.end_headers()
-
- def do_POST(self):
- self.send_response(405)
- self.send_header("Content-type", "text/plain")
- self.end_headers()
-
def log_message(self, format, *args):
if self.server.verbose:
- logging.info("%s - [%s] %s\n" % (self.address_string(),
- self.log_date_time_string(), format % args))
+ logging.info("%s - %s\n" % (self.address_string(),
+ format % args))
def getNfdStatus(self):
"""
@@ -98,9 +89,20 @@
return htmlStr
-class ThreadHTTPServer(ThreadingMixIn, HTTPServer):
- """ Handle requests using threads"""
+class ThreadHttpServer(ThreadingMixIn, HTTPServer):
+ """ Handle requests using threads """
def __init__(self, server, handler, verbose=False, robots=False):
+ serverAddr = server[0]
+ # socket.AF_UNSPEC is not supported, check whether it is v6 or v4
+ ipType = self.getIpType(serverAddr)
+ if ipType == socket.AF_INET6:
+ self.address_family = socket.AF_INET6
+ elif ipType == socket.AF_INET:
+ self.address_family == socket.AF_INET
+ else:
+ logging.error("The input IP address is neither IPv6 nor IPv4")
+ sys.exit(2)
+
try:
HTTPServer.__init__(self, server, handler)
except Exception as e:
@@ -109,6 +111,21 @@
self.verbose = verbose
self.robots = robots
+ def getIpType(self, ipAddr):
+ """ Get ipAddr's address type """
+ # if ipAddr is an IPv6 addr, return AF_INET6
+ try:
+ socket.inet_pton(socket.AF_INET6, ipAddr)
+ return socket.AF_INET6
+ except socket.error:
+ pass
+ # if ipAddr is an IPv4 addr return AF_INET, if not, return None
+ try:
+ socket.inet_pton(socket.AF_INET, ipAddr)
+ return socket.AF_INET
+ except socket.error:
+ return None
+
# main function to start
def httpServer():
@@ -116,9 +133,8 @@
parser.add_argument("-p", type=int, metavar="port number",
help="Specify the HTTP server port number, default is 8080.",
dest="port", default=8080)
- # if address is not specified, using empty str
- # so it can bind to local address
- parser.add_argument("-a", default="localhost", metavar="IP address", dest="addr",
+ # if address is not specified, use 127.0.0.1
+ parser.add_argument("-a", default="127.0.0.1", metavar="IP address", dest="addr",
help="Specify the HTTP server IP address.")
parser.add_argument("-r", default=False, dest="robots", action="store_true",
help="Enable HTTP robots to crawl; disabled by default.")
@@ -135,25 +151,31 @@
logging.basicConfig(format='%(asctime)s [%(levelname)s] %(message)s',
level=logging.INFO)
- # if port is not valid, exit
+ # if port is invalid, exit
if localPort <= 0 or localPort > 65535:
logging.error("Specified port number is invalid")
sys.exit(2)
- httpd = ThreadHTTPServer((localAddr, localPort), StatusHandler,
+ httpd = ThreadHttpServer((localAddr, localPort), StatusHandler,
verbose, robots)
+ httpServerAddr = ""
+ if httpd.address_family == socket.AF_INET6:
+ httpServerAddr = "http://[%s]:%s" % (httpd.server_address[0],
+ httpd.server_address[1])
+ else:
+ httpServerAddr = "http://%s:%s" % (httpd.server_address[0],
+ httpd.server_address[1])
- logging.info("Server Starts - at http://%s:%s" % httpd.server_address)
+ logging.info("Server started - at %s" % httpServerAddr)
try:
httpd.serve_forever()
except KeyboardInterrupt:
pass
- logging.info("Server stopping ...")
httpd.server_close()
- logging.info("Server Stopped - at http://%s:%s" % httpd.server_address)
+ logging.info("Server stopped")
if __name__ == '__main__':