

Simple web server using bash and nc - ColinWright
https://gist.github.com/dfletcher/4763be3295bd5638b797

======
alganet
Why only bash? Here is a portable (bash, dash, zsh, ksh), modular HTTP server
I did a while ago:
[https://gist.github.com/alganet/140c7c12d1603c244a01](https://gist.github.com/alganet/140c7c12d1603c244a01)

------
no_gravity
Reminds me of a simple proxy in bash that I once wrote:

mkfifo buffer; while true; do cat buffer | nc -lp 80 | tee -a client.txt | nc
domain.com 80 | tee buffer >> server.txt ; done

Useful to analyze whats going on between the browser and the server. After
running the oneliner, you can connect to domain.com via 127.0.0.1 and all data
sent from your client will be logged in client.txt and all data received from
the server will be logged in server.txt

In Firefox, you have to set network.http.max-connections to one, so Firefox
only sends one request at a time. This proxy can only handle one simultaneous
connection :)

------
andyidsinga
This is excellent as it demonstrates a bunch of concepts in a single file and
is quite readable.

thinking out loud:

This might be a great tool for certain end to end test scenarios as the script
could easily be templatized in order to pipe through sed to select difference
binary responses ... and be checked in to have a immutable point-in-time
version of the test response.

(edit: ps: I love single file pieces of code like this -- I once wrote a boot
loader and "mini-os" in a single file for an embedded board. When I started
telling people about it they loved it because they could literally read
through the code like a word document and learn how I got it all to work)

~~~
caipre
That postscript sounds interesting, is it available anywhere?

~~~
andyidsinga
I was worried someone might ask -- would have to dig it up from backup CDs :)

------
zedr
Wikipedia has a one-liner:

    
    
         { echo -ne "HTTP/1.0 200 OK\r\nContent-Length: $(wc -c <some.file)\r\n\r\n"; cat some.file; } | nc -l -p 8080
    

[https://en.wikipedia.org/wiki/Netcat](https://en.wikipedia.org/wiki/Netcat)

------
Twirrim
See also bashttpd from one of the etsy engineers:
[https://github.com/avleen/bashttpd](https://github.com/avleen/bashttpd)

------
esamatti
Remember the full web framework on Bash? Bash on Balls!

[https://github.com/jneen/balls](https://github.com/jneen/balls)

I just love the name :)

Discussion
[https://news.ycombinator.com/item?id=2781019](https://news.ycombinator.com/item?id=2781019)

------
sprash
Why not use bash internals like /dev/tcp? "nc" is rarely available on a
default installions.

~~~
benwilber0
Most OSs that I've seen shipping bash explicitly compile it without /dev/tcp
support for security reasons. I would say that nc is a far more portable
approach.

~~~
marios
Considering most OSes ship with python, I usually go with one of the
following, depending on the python version installed :

    
    
      python2 -m SimpleHTTPServer

or

    
    
      python3 -m http.server

------
zenojevski
May I also plug my own one? [https://github.com/zenoamaro/bash-
httpd](https://github.com/zenoamaro/bash-httpd)

