« Back to Index

netcat web server

View original Gist on GitHub

response.txt

HTTP/1.1 200 OK
Content-Type: text/html
Connection: keep-alive

<p>Stuff</p>

server.sh

#!/bin/bash

RESPONSE="HTTP/1.1 200 OK\r\nConnection: keep-alive\r\n\r\n${2:-"OK"}\r\n"

while { echo -en "$RESPONSE"; } | /usr/bin/nc -l "${1:-8080}"; do
  echo "================================================"
done

# Note: using GNU netcat doesn't work, not sure why (so we use macOS' netcat)

term1.sh

sudo /usr/bin/nc -l 80 < response.txt

# Note: using GNU netcat doesn't work, not sure why (so we use macOS' netcat)

term2.sh

curl -v 127.0.0.1:80

* Rebuilt URL to: 127.0.0.1:80/
*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to 127.0.0.1 (127.0.0.1) port 80 (#0)
> GET / HTTP/1.1
> Host: 127.0.0.1
> User-Agent: curl/7.50.3
> Accept: */*
> 
< HTTP/1.1 200 OK
< Date: Mon, 27 Jul 2009 12:28:53 GMT
< Server: Apache/2.2.14 (Win32)
< Last-Modified: Wed, 22 Jul 2009 19:15:56 GMT
< Content-Length: 88
< Content-Type: text/html
< Connection: Closed
< 
<p>Stuff</p>
* transfer closed with 75 bytes remaining to read
* Curl_http_done: called premature == 1
* stopped the pause stream!
* Closing connection 0
curl: (18) transfer closed with 75 bytes remaining to read