3 Copyright (c) 2014-2024, Regents of the University of California,
4 Arizona Board of Regents,
5 Colorado State University,
6 University Pierre & Marie Curie, Sorbonne University,
7 Washington University in St. Louis,
8 Beijing Institute of Technology,
9 The University of Memphis.
11 This file is part of NFD (Named Data Networking Forwarding Daemon).
12 See AUTHORS.md for complete list of NFD authors and contributors.
14 NFD is free software: you can redistribute it and/or modify it under the terms
15 of the GNU General Public License as published by the Free Software Foundation,
16 either version 3 of the License, or (at your option) any later version.
18 NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19 without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 PURPOSE. See the GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License along with
23 NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
31 from http.server
import SimpleHTTPRequestHandler, ThreadingHTTPServer
32 from urllib.parse
import urlsplit
36 """The handler class to handle HTTP requests."""
39 path = urlsplit(self.path).path
42 elif path ==
"/robots.txt" and self.server.allow_robots:
47 def __serve_report(self):
48 """Obtain XML-formatted NFD status report and send it back as response body."""
50 proc = subprocess.run(
51 [
"nfdc",
"status",
"report",
"xml"], capture_output=
True, check=
True, text=
True, timeout=10
54 except OSError
as err:
57 except subprocess.SubprocessError
as err:
59 self.send_error(504,
"Cannot connect to NFD")
63 if (pos := output.find(
">") + 1) != 0:
64 xml = output[:pos] +
'<?xml-stylesheet type="text/xsl" href="nfd-status.xsl"?>' + output[pos:]
65 self.send_response(200)
66 self.send_header(
"Content-Type",
"text/xml; charset=UTF-8")
68 self.wfile.write(xml.encode())
70 super().
log_message(f
"malformed nfdc output: {output}")
75 if self.server.verbose:
80 def __init__(self, addr, port, *, robots=False, verbose=False):
87 super().
__init__((str(addr), port), NfdStatusHandler)
91 def ip_address(arg, /):
92 """Validate IP address."""
94 value = ipaddress.ip_address(arg)
96 raise argparse.ArgumentTypeError(f
"{arg!r} is not a valid IP address")
99 def port_number(arg, /):
100 """Validate port number."""
105 if value < 0
or value > 65535:
106 raise argparse.ArgumentTypeError(f
"{arg!r} is not a valid port number")
109 parser = argparse.ArgumentParser(description=
"Serves NFD status page via HTTP")
110 parser.add_argument(
"-V",
"--version", action=
"version", version=
"@VERSION@")
111 parser.add_argument(
"-a",
"--address", default=
"127.0.0.1", type=ip_address, metavar=
"ADDR",
112 help=
"bind to this IP address (default: %(default)s)")
113 parser.add_argument(
"-p",
"--port", default=6380, type=port_number,
114 help=
"bind to this port number (default: %(default)s)")
115 parser.add_argument(
"-f",
"--workdir", default=
"@DATAROOTDIR@/ndn", metavar=
"DIR",
116 help=
"server's working directory (default: %(default)s)")
117 parser.add_argument(
"-r",
"--robots", action=
"store_true", help=
"allow crawlers and other HTTP bots")
118 parser.add_argument(
"-v",
"--verbose", action=
"store_true", help=
"turn on verbose logging")
119 args = parser.parse_args()
121 os.chdir(args.workdir)
123 with NfdStatusHttpServer(args.address, args.port, robots=args.robots, verbose=args.verbose)
as httpd:
124 if httpd.address_family == socket.AF_INET6:
125 url =
"http://[{}]:{}"
128 print(
"Server started at", url.format(*httpd.server_address))
131 httpd.serve_forever()
132 except KeyboardInterrupt:
136 if __name__ ==
"__main__":
def log_message(self, *args)
def __init__(self, addr, port, *robots=False, verbose=False)