Minimal web server using netcat

后端 未结 14 782
借酒劲吻你
借酒劲吻你 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:40

    Another way to do this

    while true; do (echo -e 'HTTP/1.1 200 OK\r\n'; echo -e "\n\tMy website has date function" ; echo -e "\t$(date)\n") | nc -lp 8080; done
    

    Let's test it with 2 HTTP request using curl

    In this example, 172.16.2.6 is the server IP Address.

    Server Side

    admin@server:~$ while true; do (echo -e 'HTTP/1.1 200 OK\r\n'; echo -e "\n\tMy website has date function" ; echo -e "\t$(date)\n") | nc -lp 8080; done
    
    GET / HTTP/1.1 Host: 172.16.2.6:8080 User-Agent: curl/7.48.0 Accept:
    */*
    
    GET / HTTP/1.1 Host: 172.16.2.6:8080 User-Agent: curl/7.48.0 Accept:
    */*
    

    Client Side

    user@client:~$ curl 172.16.2.6:8080
    
            My website has date function
            Tue Jun 13 18:00:19 UTC 2017
    
    user@client:~$ curl 172.16.2.6:8080
    
            My website has date function
            Tue Jun 13 18:00:24 UTC 2017
    
    user@client:~$
    

    If you want to execute another command, feel free to replace $(date).

提交回复
热议问题