Friday, November 13, 2015

bash: single line webserver with nc

setup a single line webserver in bash for test purposes can be very easy.

1) put the response page in a file
echo "<html><br>x1622 test webserver</br></html>">/tmp/response.txt

2) get the size of the file
ls -al /tmp/response.txt

3) start a simple webserver in bash using nc
while true; do { echo -e 'HTTP/1.1 200 OK\r\nContent-Length:43\r\nContent-Type:text/html\r\n'; cat /tmp/response.txt; } | sudo nc -l -p 80 -q 1; done

Other Content Types:
Content-Type="text/html", "image/png", "image/gif", "video/mpeg", "text/css", "text/plain"

4) call webpage with browser

5) webserver start & log