Introduction
Sometimes when our application is not ready for devployment, but we hope to setup a environment to test the network connectivity, host a simple test server is a really usefull. If your server or container contains python, it will be very easy with command like this python -m http.server 8000
to achieve this. but if your environment does not have python. Then it also has a way to create a simple server.
Create a simple server with shell script
On Unix-like operating systems, the nc
command runs Netcat, a utility for sending raw data over a network connection. We can use nc
coomand to host a http server.
#!/bin/sh
# response
response() {
echo "HTTP/1.0 200 OK"
echo "Content-Type: text/plain"
echo ""
echo "Hello"
}
# listen ctrl-c
trap exit INT
# loop and listen port 8000
# if you want to listen IPv6, use -l6
while true; do
response | nc -l 8000 -w 1
done