trnbl.loggers.local.start_server API documentation

docs for trnbl v0.1.1
View Source on GitHub

# trnbl.loggers.local.start_server

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

View Source
View on GitHub

     1"Usage: python start_server.py path/to/directory [port]"
     2
     3import os
     4import http.server
     5import socketserver
     6
     7
     8def 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
    18if __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)

def start_server(path: str, port: int = 8000) -> None:
View Source
View on GitHub

     9def start_server(path: str, port: int = 8000) -> None:
    10	"""Starts a server to serve the files in the given path."""
    11	os.chdir(path)
    12	with socketserver.TCPServer(
    13		("", port), http.server.SimpleHTTPRequestHandler
    14	) as httpd:
    15		print(f"Serving at http://localhost:{port}")
    16		httpd.serve_forever()

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