Minimal web server using netcat

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

    I think the problem that all the solution listed doesn't work, is intrinsic in the nature of http service, the every request established is with a different client and the response need to be processed in a different context, every request must fork a new instance of response...

    The current solution I think is the -e of netcat but I don't know why doesn't work... maybe is my nc version that I test on openwrt...

    with socat it works....

    I try this https://github.com/avleen/bashttpd

    and it works, but I must run the shell script with this command.

    socat tcp-l:80,reuseaddr,fork EXEC:bashttpd &
    

    The socat and netcat samples on github doesn't works for me, but the socat that I used works.

    0 讨论(0)
  • 2020-12-04 05:47

    LOL, a super lame hack, but at least curl and firefox accepts it:

    while true ; do (dd if=/dev/zero count=10000;echo -e "HTTP/1.1\n\n $(date)") | nc -l  1500  ; done
    

    You better replace it soon with something proper!

    Ah yes, my nc were not exactly the same as yours, it did not like the -p option.

    0 讨论(0)
  • 2020-12-04 05:52

    Donno how or why but i manage to find this around and it works for me, i had the problem I wanted to return the result of executing a bash

    $ while true; do { echo -e 'HTTP/1.1 200 OK\r\n'; sh test; } | nc -l 8080; done
    

    NOTE: This command was taken from: http://www.razvantudorica.com/08/web-server-in-one-line-of-bash

    this executes bash script test and return the result to a browser client connecting to the server running this command on port 8080

    My script does this ATM

    $ nano test
    
    #!/bin/bash
    
    echo "************PRINT SOME TEXT***************\n"
    echo "Hello World!!!"
    echo "\n"
    
    echo "Resources:"
    vmstat -S M
    echo "\n"
    
    echo "Addresses:"
    echo "$(ifconfig)"
    echo "\n"
    
    
    echo "$(gpio readall)"
    

    and my web browser is showing

    ************PRINT SOME TEXT***************
    
    Hello World!!!
    
    
    Resources:
    procs -----------memory---------- ---swap-- -----io---- -system-- ----cpu----
     r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa
     0  0      0    314     18     78    0    0     2     1  306   31  0  0 100  0
    
    
    Addresses:
    eth0      Link encap:Ethernet  HWaddr b8:27:eb:86:e8:c5  
              inet addr:192.168.1.83  Bcast:192.168.1.255  Mask:255.255.255.0
              UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
              RX packets:27734 errors:0 dropped:0 overruns:0 frame:0
              TX packets:26393 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:1000 
              RX bytes:1924720 (1.8 MiB)  TX bytes:3841998 (3.6 MiB)
    
    lo        Link encap:Local Loopback  
              inet addr:127.0.0.1  Mask:255.0.0.0
              UP LOOPBACK RUNNING  MTU:65536  Metric:1
              RX packets:0 errors:0 dropped:0 overruns:0 frame:0
              TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:0 
              RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)
    
    
    GPIOs:
    +----------+-Rev2-+------+--------+------+-------+
    | wiringPi | GPIO | Phys | Name   | Mode | Value |
    +----------+------+------+--------+------+-------+
    |      0   |  17  |  11  | GPIO 0 | IN   | Low   |
    |      1   |  18  |  12  | GPIO 1 | IN   | Low   |
    |      2   |  27  |  13  | GPIO 2 | IN   | Low   |
    |      3   |  22  |  15  | GPIO 3 | IN   | Low   |
    |      4   |  23  |  16  | GPIO 4 | IN   | Low   |
    |      5   |  24  |  18  | GPIO 5 | IN   | Low   |
    |      6   |  25  |  22  | GPIO 6 | IN   | Low   |
    |      7   |   4  |   7  | GPIO 7 | IN   | Low   |
    |      8   |   2  |   3  | SDA    | IN   | High  |
    |      9   |   3  |   5  | SCL    | IN   | High  |
    |     10   |   8  |  24  | CE0    | IN   | Low   |
    |     11   |   7  |  26  | CE1    | IN   | Low   |
    |     12   |  10  |  19  | MOSI   | IN   | Low   |
    |     13   |   9  |  21  | MISO   | IN   | Low   |
    |     14   |  11  |  23  | SCLK   | IN   | Low   |
    |     15   |  14  |   8  | TxD    | ALT0 | High  |
    |     16   |  15  |  10  | RxD    | ALT0 | High  |
    |     17   |  28  |   3  | GPIO 8 | ALT2 | Low   |
    |     18   |  29  |   4  | GPIO 9 | ALT2 | Low   |
    |     19   |  30  |   5  | GPIO10 | ALT2 | Low   |
    |     20   |  31  |   6  | GPIO11 | ALT2 | Low   |
    +----------+------+------+--------+------+-------+
    

    simply amazing!

    0 讨论(0)
  • 2020-12-04 05:54

    Try this:

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

    The -cmakes netcat execute the given command in a shell, so you can use echo. If you don't need echo, use -e. For further information on this, try man nc. Note, that when using echo there is no way for your program (the date-replacement) to get the browser request. So you probably finally want to do something like this:

    while true ; do nc -l -p 1500 -e /path/to/yourprogram ; done
    

    Where yourprogram must do the protocol stuff like handling GET, sending HTTP 200 etc.

    0 讨论(0)
  • 2020-12-04 05:56

    Type in nc -h and see if You have -e option available. If yes, You can create a script, for example:

    script.sh

    echo -e "HTTP/1.1 200 OK\n\n $(date)"
    

    and run it like this:

    while true ; do nc -l -p 1500 -e script.sh; done
    

    Note that -e option needs to be enabled at compilation to be available.

    0 讨论(0)
  • 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;
    
    0 讨论(0)
提交回复
热议问题