Minimal web server using netcat

后端 未结 14 780
借酒劲吻你
借酒劲吻你 2020-12-04 05:15

I\'m trying to set up a minimal web server using netcat (nc). When the browser calls up localhost:1500, for instance, it should show the result of a function (date

14条回答
  •  执笔经年
    2020-12-04 05:57

    Actually, the best way to close gracefully the connection is to send the Content-Length header like following. Client (like curl will close the connection after receiving the data.

    DATA="Date: $(date)"; 
    LENGTH=$(echo $DATA | wc -c);
    echo -e "HTTP/1.1 200 OK\nContent-Length: ${LENGTH}\n\n${DATA}" | nc -l -p 8000;
    

提交回复
热议问题