ยปCore Development>Code coverage>Tools/scripts/serve.py

Python code coverage for Tools/scripts/serve.py

#countcontent
1n/a#!/usr/bin/env python3
2n/a'''
3n/aSmall wsgiref based web server. Takes a path to serve from and an
4n/aoptional port number (defaults to 8000), then tries to serve files.
5n/aMime types are guessed from the file names, 404 errors are raised
6n/aif the file is not found. Used for the make serve target in Doc.
7n/a'''
8n/aimport sys
9n/aimport os
10n/aimport mimetypes
11n/afrom wsgiref import simple_server, util
12n/a
13n/adef app(environ, respond):
14n/a
15n/a fn = os.path.join(path, environ['PATH_INFO'][1:])
16n/a if '.' not in fn.split(os.path.sep)[-1]:
17n/a fn = os.path.join(fn, 'index.html')
18n/a type = mimetypes.guess_type(fn)[0]
19n/a
20n/a if os.path.exists(fn):
21n/a respond('200 OK', [('Content-Type', type)])
22n/a return util.FileWrapper(open(fn, "rb"))
23n/a else:
24n/a respond('404 Not Found', [('Content-Type', 'text/plain')])
25n/a return [b'not found']
26n/a
27n/aif __name__ == '__main__':
28n/a path = sys.argv[1]
29n/a port = int(sys.argv[2]) if len(sys.argv) > 2 else 8000
30n/a httpd = simple_server.make_server('', port, app)
31n/a print("Serving {} on port {}, control-C to stop".format(path, port))
32n/a try:
33n/a httpd.serve_forever()
34n/a except KeyboardInterrupt:
35n/a print("\b\bShutting down.")