Add a --listen option to the python script
which will start a simple python http server to serve the generated files
This commit is contained in:
parent
2fb5f0ea62
commit
a5a0cf43b8
28
genstatic.py
28
genstatic.py
|
@ -3,11 +3,23 @@
|
||||||
# directory that was passed as a command line argument
|
# directory that was passed as a command line argument
|
||||||
import sys
|
import sys
|
||||||
import os
|
import os
|
||||||
|
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
# check if the user supplied the correct number of command line arguments
|
# check if the user supplied the correct number of command line arguments
|
||||||
if len(sys.argv) != 2:
|
arglen = len(sys.argv)
|
||||||
print("Usage: python index.py htmlfilesdirectory")
|
if arglen !=2 and arglen != 4:
|
||||||
|
print("Usage: python index.py htmlfilesdirectory [--listen] [port]")
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
listen = False
|
||||||
|
port = "8000"
|
||||||
|
if arglen == 4:
|
||||||
|
if sys.argv[2] == "--listen":
|
||||||
|
listen = True
|
||||||
|
port = sys.argv[3]
|
||||||
|
else:
|
||||||
|
print("Usage: python index.py htmlfilesdirectory [--listen] [port]")
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
# set the directory from the command line argument
|
# set the directory from the command line argument
|
||||||
|
@ -36,3 +48,15 @@ for file in sorted(os.listdir(dir)):
|
||||||
#write a </html> tag to the file
|
#write a </html> tag to the file
|
||||||
outfile.write("</html>")
|
outfile.write("</html>")
|
||||||
outfile.close
|
outfile.close
|
||||||
|
|
||||||
|
# wait 1 second
|
||||||
|
import time
|
||||||
|
time.sleep(1)
|
||||||
|
|
||||||
|
fullpath = os.path.abspath(dir)
|
||||||
|
if listen:
|
||||||
|
# run a simple python http server to serve the files using SimpleHTTPServer
|
||||||
|
# the server will serve files from the directory that was passed as a command line argument
|
||||||
|
# the server will run in the background
|
||||||
|
import subprocess
|
||||||
|
subprocess.Popen(["python3", "-m", "http.server", port, "--directory", fullpath], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
||||||
|
|
Loading…
Reference in New Issue