A quick dirty netcat web server

Here’s a quick and dirty netcat web server that listens for an HTTP request and responds with HTTP Headers. It listens (-l flag) to port 8001 and quits/terminates the response (-q0) after 0 seconds of EOF on stdin.

This can be quite handy but of course it would not provide all the features available of a popular web server.

I’ve used netcat openbsd .

The bash script is as follows:

#!/bin/bash

port=8001
responseMsg="HTTP/1.1 200 OK\r\n\r\nOK"

while true; do
  echo -e "$responseMsg" | nc -q0 -l "$port"
done

Alternatively, you may opt to do a one liner as follows:

while true; do echo -e "HTTP/1.1 200 OK\r\n\r\nOK" | nc -q0 -l <PORT>

Make an HTTP GET request on port 8001:

curl localhost:8001

Response:

OK
comments powered by Disqus