File: simple-http-server.sh

package info (click to toggle)
libreswan 4.3-1%2Bdeb11u4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 62,688 kB
  • sloc: ansic: 108,293; sh: 25,973; xml: 11,756; python: 10,230; makefile: 1,580; javascript: 1,353; yacc: 825; sed: 647; perl: 584; lex: 159; awk: 156
file content (52 lines) | stat: -rwxr-xr-x 1,138 bytes parent folder | download | duplicates (3)
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
#!/bin/sh

if test $# -lt 2 ; then
    cat <<EOF > /dev/stderr

Usage:

    $0 <directory> <port> [ <http-arg> ... ]

start a background python3 http.server in <directory> listening to
<port>

EOF

    exit 1
fi

directory=$1 ; shift
port=$1 ; shift
logfile=simple-http-server.log
pidfile=simple-http-server.pid

cd ${directory}

# Start the server in the background.
#
# Force un-buffered output so that the start-up message is immediately
# written to the log file (without it the log file remains empty).

python3 -u -m http.server ${port} "$@" > ${logfile} 2>&1 &
echo $! > ${pidfile}

# Wait for the server to start.  Check for both the "Serving ..." log
# line and an open port.

i=0
while true ; do
    if test -s ${logfile} && ncat 127.0.0.1 ${port} < /dev/null 2>/dev/null ; then
	# Strip off f28's extra text:
	# f22: Serving HTTP on 0.0.0.0 port 80 ...
	# f28: Serving HTTP on 0.0.0.0 port 80 (http://0.0.0.0:80/) ...
	sed -e 's; (http://[^)]*);;' simple-http-server.log
	exit 0
    fi
    i=$((i + 1))
    test $i -lt 5 || break
    sleep 1
done

echo Timeout waiting for HTTP server on ${port} to start
cat ${logfile}
exit 1