Minimal web server using netcat

后端 未结 14 693
借酒劲吻你
借酒劲吻你 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:59
    while true; do (echo -e 'HTTP/1.1 200 OK\r\nConnection: close\r\n';) | timeout 1  nc -lp 8080 ; done
    

    Closes connection after 1 sec, so curl doesn't hang on it.

    0 讨论(0)
  • 2020-12-04 06:04

    If you're using Apline Linux, the BusyBox netcat is slightly different:

    while true; do nc -l -p 8080 -e sh -c 'echo -e "HTTP/1.1 200 OK\n\n$(date)"'; done
    

    And another way using printf:

    while true; do nc -l -p 8080 -e sh -c "printf 'HTTP/1.1 200 OK\n\n%s' \"$(date)\""; done
    
    0 讨论(0)
提交回复
热议问题