Skip to content

Instantly share code, notes, and snippets.

@coderofsalvation
Last active January 2, 2016 19:09
Show Gist options
  • Select an option

  • Save coderofsalvation/8348533 to your computer and use it in GitHub Desktop.

Select an option

Save coderofsalvation/8348533 to your computer and use it in GitHub Desktop.
simple server listening on port
# simple server listening on port
# for extra (http) functions see: https://github.com/coderofsalvation/bashwapp/blob/master/app
# @dependancy nc
# usage: POST=8000 listenport
# telnet localhost 8000
[[ ! -n $PORT ]] && PORT=8000 # allow port to be passed on commandline
listenport(){
TMPFILE="/tmp/.myapp.$(whoami)"; CLIENT="$TMPFILE.fifo.$PORT.$(whoami)"
[[ ! -p $CLIENT ]] && mkfifo $CLIENT
while [[ -p $CLIENT ]]; do cat $CLIENT | nc -v -l $PORT 2>&1 | onRequest "$CLIENT"; done
rm $TMPFILE.*
}
onRequest(){
request="$(cat - )"
echo "OK\r\n" > "$1"
echo "incoming = $request"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment