-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathrun_web_server.sh
More file actions
executable file
·78 lines (63 loc) · 2.1 KB
/
run_web_server.sh
File metadata and controls
executable file
·78 lines (63 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env bash
# Usage: ./run_web_server.sh [PORT] [DOCROOT]
# Defaults: PORT=8000, DOCROOT=.
# - Kills any existing process listening on PORT
# - Then starts `python3 -m http.server PORT` in DOCROOT
set -euo pipefail
PORT="${1:-8000}"
DOCROOT="${2:-.}"
echo "running serever on http://localhost:${PORT}"
echo "serving from ${DOCROOT}"
# Kill anything already on PORT (prefer lsof; fall back to fuser)
kill_listen(){
local pids
pids="$(lsof -tiTCP:"${PORT}" -sTCP:LISTEN 2>/dev/null || true)"
if [[ -n "${pids}" ]]; then
echo "Killing existing server(s) on port ${PORT}: ${pids}" >&2
kill ${pids} 2>/dev/null || true
return 0
fi
if command -v fuser >/dev/null 2>&1; then
# fuser prints PIDs to stderr
if fuser -k "${PORT}/tcp" >/dev/null 2>&1; then
echo "Killed via fuser on port ${PORT}" >&2
return 0
fi
fi
return 1
}
for _ in {1..5}; do
kill_listen || true
sleep 0.2
if ! lsof -tiTCP:"${PORT}" -sTCP:LISTEN >/dev/null 2>&1; then
break
fi
done
if lsof -tiTCP:"${PORT}" -sTCP:LISTEN >/dev/null 2>&1; then
echo "Port ${PORT} still busy after kill attempts. Aborting." >&2
exit 1
fi
# Get the directory of this script
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
# Create a python script to run the server with no-cache headers
cat <<EOF > "$SCRIPT_DIR/server.py"
import http.server
import socketserver
import sys
PORT = int(sys.argv[1]) if len(sys.argv) > 1 else 8000
class NoCacheHTTPRequestHandler(http.server.SimpleHTTPRequestHandler):
def end_headers(self):
self.send_header('Cache-Control', 'no-store, no-cache, must-revalidate, max-age=0')
self.send_header('Pragma', 'no-cache')
self.send_header('Expires', '0')
super().end_headers()
with socketserver.TCPServer(("", PORT), NoCacheHTTPRequestHandler) as httpd:
print(f"Serving at port {PORT} with no-cache headers")
try:
httpd.serve_forever()
except KeyboardInterrupt:
pass
EOF
echo "Starting custom no-cache server on port ${PORT} in ${DOCROOT}" >&2
cd "${DOCROOT}"
exec python3 "$SCRIPT_DIR/server.py" "${PORT}"