Skip to content

Instantly share code, notes, and snippets.

@nfiles
Created December 18, 2023 16:59
Show Gist options
  • Save nfiles/bdbbd1c69a2afc76ab9a47114c858c44 to your computer and use it in GitHub Desktop.
Save nfiles/bdbbd1c69a2afc76ab9a47114c858c44 to your computer and use it in GitHub Desktop.
simple hello world server with netcat
#! /bin/bash
set -e
set -o pipefail
usage() {
echo "$0 [--port <PORT>]"
}
PORT=8888
while ARG="$1"; shift; do
case "$ARG" in
"--port" | "-p") PORT="$1"; shift ;;
"--help" | "-h") usage; exit 0 ;;
*) usage; exit 1 ;;
esac
done
while true; do
printf "HTTP/1.1 200 OK\r\nHost: 127.0.0.1:$PORT\r\nServer: netcat!\r\nContent-Type: application/json; charset-UTF-8\r\nContent-Length: 34\r\n\r\n{ \"message\": \"Hello, world!\" }\r\n\r\n" \
| netcat -l "$PORT"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment