4 ms·
Simple web server using bash and nc
- sprash 11y agoWhy not use bash internals like /dev/tcp? "nc" is rarely available on a default installions.
- benwilber0 11y agoMost 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 11y agoConsidering 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
- sprash 11y agoAll machines I have access right now have /dev/tcp support. But only my main machine has nc installed. To make it more portable a wrapper function which uses whatever is available would be appropriate.
- alganet 11y agoWhy 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
- Twirrim 11y agoSee also bashttpd from one of the etsy engineers: https://github.com/avleen/bashttpd https://github.com/avleen/bashttpd
- no_gravity 11y agoReminds 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 :)
- zedr 11y agoWikipedia 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
- andyidsinga 11y agoThis 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 11y agoThat postscript sounds interesting, is it available anywhere?
- andyidsinga 11y agoI was worried someone might ask -- would have to dig it up from backup CDs :)
- esamatti 11y agoRemember 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
- zenojevski 11y agoMay I also plug my own one? https://github.com/zenoamaro/bash-httpd https://github.com/zenoamaro/bash-httpd