Web server in one line of bash

If you want to quickly save a file through http but you don't want to install a web server, you can just use netcat.
You can run:

  1. while true; do { echo -e 'HTTP/1.1 200 OK\r\n'; cat index.html; } | nc -l 8080; done

index.html can be any file you want to serve it.

You can access it after that as: http://host_ip:8080/

Comments

For that purpose I'm using:

rm -f out ; mkfifo out ; while true ; do cat out | nc -l localhost 8080 | tr -ud '\r' | awk '/^GET /{system("cat "$2">out")}' ; done

Nice. Using netcat would be called cheating though, no?

Doesn't require root to run!

This one is longer. http://en.wikipedia.org/wiki/Netcat#Setting_up_a_one-shot_webserver_on_p... -
it also provides the Content-Length to clients.

Cheers!

It's not cheating. He's using an unreserved-port.

Or you can use Python: python -m SimpleHTTPServer
Or you can use Twisted: twistd -n web --path .

There is a good chance that you have Twisted already installed as a dependency of something else. Otherwise the Python module will serve you well (the Twisted version can serve multiple requests concurrently).

You don't need root for port 8080; only if you want port < 1024.

If your netcat is new enough, you can also use:
h(){ nc -lp8080 -c'r=HTTP/1.0;f=`head -n1|cut -d\ -f2`;[ -f .$f ]&&echo "$r 200\nContent-Type: `file -bi .$f`\n"&&cat .$f||echo $r 404';h;}

which fits in a tweet :)

php -S
Will also do the trick, if you've got a recent php version installed. It will of course serve php-files, which can be both good and bad. :P

RT @Linuzifer: Webserver in einer Zeile, bash. Alles andere ist Overhead. http://t.co/Qb8fIkhPqo

Webserver mit nur einer Zeile Shell-Script:

http://t.co/dU2LKh4irA http://t.co/SvvyEc7ern

RT @Linuzifer: Webserver in einer Zeile, bash. Alles andere ist Overhead. http://t.co/Qb8fIkhPqo

いいね / “Web server in one line of bash | Răzvan Tudorică” http://t.co/vZZgP7IFYZ

RT @Linuzifer: Webserver in einer Zeile, bash. Alles andere ist Overhead. http://t.co/Qb8fIkhPqo

http://t.co/92p3oDlv6n
Покруче #nginx будет.

Web server in one line of bash http://t.co/hM0z0xW2uH

Web Server in One line of Bash - http://t.co/LqwWW0eJ7x

Web server in one line of bash (http://t.co/pLculpdb32) http://t.co/rGTxGDKr1q

RT @Linuzifer: Webserver in einer Zeile, bash. Alles andere ist Overhead. http://t.co/Qb8fIkhPqo

Web server in one line of bash http://t.co/cVXQuCLrGy

Web server in one line of bash http://t.co/TQ8FYkuJ1o (http://t.co/s5XUpE8ewa)

Cool! RT @cballou: A simple web server via one line of bash that can fit in a tweet you say? Nice. http://t.co/DdhJIaTWX1

A simple web server via one line of bash that can fit in a tweet you say? Nice. http://t.co/N5xHk02jVT

Web server in one line of bash | Răzvan Tudorică http://t.co/17hZNozSZo

noch ein Beispiel für schlichte Eleganz auf der Kommandezeile - der Einzeiler Webserver #linux #bash http://t.co/HiFh83rTZ3

Web server in one line of bash http://t.co/7psNreDJ71 See also comments for more ideas

RT @Linuzifer: Webserver in einer Zeile, bash. Alles andere ist Overhead. http://t.co/Qb8fIkhPqo

Web server in one line of bash | Răzvan Tudorică http://t.co/YZsNhKkZok

Web server in one line of bash http://t.co/T2JtkBvT3Z

RT @JamiesonBecker: Totally awesome.
A web server in ONE LINE of bash:
http://t.co/9CM8Fgu7Ij

RT @JamiesonBecker: Totally awesome.
A web server in ONE LINE of bash:
http://t.co/9CM8Fgu7Ij

RT @JamiesonBecker: Totally awesome.
A web server in ONE LINE of bash:
http://t.co/9CM8Fgu7Ij

Totally awesome.
A web server in ONE LINE of bash:
http://t.co/9CM8Fgu7Ij

Run a webserver in a single line of bash. http://t.co/OXIiYlLFxw #bash #scripting #web

A web server in one line of bash http://t.co/z19Pi1m80d

Web server in one line of bash | Răzvan Tudorică http://t.co/PF9D9gZFhg

I prefer "python -m SimpleHTTPServer [port]" :P RT @hnycombinator: Web server in one line of bash http://t.co/1Wp546Ua7d

Web server in one line of bash using netcat http://t.co/kBuXvAIf9B

Web server in one line of bash http://t.co/l0UgZ6vHUx

RT @Linuzifer: Webserver in einer Zeile, bash. Alles andere ist Overhead. http://t.co/Qb8fIkhPqo

Web server in one line of bash | Răzvan Tudorică http://t.co/4QFG7XIcQv

Way fun, Turns out it can server an image too. Replace index.html with file.jpg (png gif) and you get the image displayed as well. Obvious, but neat to see the image. Great on razvan. I've used inet to do this with cat, first time in bash. cool.

Web server in one line of bash http://t.co/Jl6pfMq8JJ

Web server in one line of bash http://t.co/ooympQJjra

RT @JamiesonBecker: Totally awesome.
A web server in ONE LINE of bash:
http://t.co/9CM8Fgu7Ij

RT @AngeZanetti: Web server in one line of bash | Răzvan Tudorică http://t.co/4QFG7XIcQv

RT @Linuzifer: Webserver in einer Zeile, bash. Alles andere ist Overhead. http://t.co/Qb8fIkhPqo

RT @JamiesonBecker: Totally awesome.
A web server in ONE LINE of bash:
http://t.co/9CM8Fgu7Ij

RT @newsycombinator: Web server in one line of bash http://t.co/GbSC1aWzCX

Pages

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.