Coverage for trnbl\loggers\local\start_server.py: 28%

1"Usage: python start_server.py path/to/directory [port]"

2

3 import os

4 import http.server

5 import socketserver

6

7

8 def start_server(path: str, port: int = 8000) -> None:

9 """Starts a server to serve the files in the given path."""

10 os.chdir(path)

11 with socketserver.TCPServer(

12 ("", port), http.server.SimpleHTTPRequestHandler

13 ) as httpd:

14 print(f"Serving at http://localhost:{port}")

15 httpd.serve_forever()

16

17

18 if __name__ == "__main__":

19 import sys

20

21 if len(sys.argv) == 1:

22 start_server(".")

23 elif len(sys.argv) == 2:

24 start_server(sys.argv[1])

25 elif len(sys.argv) == 3:

26 start_server(sys.argv[1], int(sys.argv[2]))

27 else:

28 print(f"Invalid number of arguments!\n{__doc__}")

29 sys.exit(1)