8 ms·
Web server in one line of bash
- mcantrell 13y agoNeat trick, but probably too limited to be of much use. Also, stopping it is kind of a nuisance.
- brokenparser 13y agoWhy? It only takes one ^C
- kibibyte 13y agoFor me, it doesn't even respond to ^C (or even ^\). I had to ^Z it, and then `kill %1`. Could be an OS-dependent thing. I'm using OS X.
- Keyframe 13y agoIndeed. It could use in front of while loop something like: int_cmd(){exit 1;}trap 'int_cmd' 2;
- brokenparser 13y agoYou get what you pay for.
- WestCoastJustin 13y agoIf you have python, then these are really useful, just serves up the current directory. Python 2.x: python -m SimpleHTTPServer Python 3.x: python -m http.server 8000
- sprobertson 13y agoEasier to remember, plus Python is probably more likely to exist on a given machine than netcat.
- mikegirouard 13y agoThis is still my go-to solution, though I couldn't help but to share when I found the other solution.
- deleted 13y ago[deleted]
- gmac 13y agoOr for https (e.g. when developing for Chrome's speech recognition API): twistd --nodaemon web --path=. -c snakeoil.crt -k snakeoil.key --https=8443 And to generate a 100-year localhost key/cert for use with this: openssl genrsa -passout pass:dummy -out snakeoil.secure.key 1024 openssl rsa -passin pass:dummy -in snakeoil.secure.key -out snakeoil.key openssl req -new -subj "/commonName=localhost" -key snakeoil.key -out snakeoil.csr openssl x509 -req -days 36500 -in snakeoil.csr -signkey snakeoil.key -out snakeoil.crt
- e12e 13y agoOr: cat /etc/certs/ssl-cert-snakeoil.pem \ /etc/priv/ssl-cert-snakeoil.key >> \ /etc/priv/ssl-cert-and-key-snakeoil.pem chmod og-rwx /etc/priv/ssl-cert-and-key-snakeoil.pem #note instructions above on how to generate a fresh #self-signed cert python3 -m http.server 8000 & stunnel -p /etc/ssl/private/ssl-cert-and-key-snakeoil.pem -r 8000 -d 4430 Which does require stunnel, in addition to python, obviously.
- gmac 13y agoI can't edit my original comment now, but thought I should add this for cleanup after key/cert generation: rm snakeoil.secure.key snakeoil.csr
- Sven7 13y agoerm...chrome speech API?!? What exactly are you doing? and how can I do it too?
- gmac 13y agoI'm doing: https://github.com/jawj/pigeonsim https://github.com/jawj/pigeonsim (now with speech recognition + geocoding, supporting a beam-me-up gesture). Web speech API: http://updates.html5rocks.com/2013/01/Voice-Driven-Web-Apps-Introduction-to-the-Web-Speech-API http://updates.html5rocks.com/2013/01/Voice-Driven-Web-Apps-...
- RobAley 13y agoOr if you have PHP, there is an equivalent in 5.4 and above. http://php.net/manual/en/features.commandline.webserver.php http://php.net/manual/en/features.commandline.webserver.php
- knome 13y ago> As root you can run: Unless the specified 'index.html' is only readable by root, you don't have to be root to run that. Only the low ports are restricted from regular users.
- fjcaetano 13y agoQuite interesting, specially with the limitations of the file:// protocol
- deleted 13y ago[deleted]
- swirepe 13y agoI like woof,which is a single file and can also serve itself. http://www.home.unix-ag.org/simon/woof.html http://www.home.unix-ag.org/simon/woof.html
- anakron 13y agoWe've used a similar trick to copy files between coworkers: on server: while true; nc -l $PORT < $FILE; done on client: nc $SERVER $PORT > $FILE Quick, simple and easy to fire up. You can continue to hack on the file and nc will serve up the latest saved version.
- gamegoblin 13y agoIs your while loop missing a "do" statement, or am I missing something?
- human_error 13y agoAnother one: http://paulbuchheit.blogspot.com/2007/04/webserver-in-bash.html http://paulbuchheit.blogspot.com/2007/04/webserver-in-bash.h...
- hyperair 13y ago$ busybox httpd Works even in an initramfs, and in some embedded systems. Serves up the current directory, and you can even have CGI scripts in cgi-bin.
- voltagex_ 13y agoThanks! I'd forgotten about busybox, but that's often how your home router is doing things. * Side note: if it is, make sure your router manufacturer provides the GPL source release.
- justincormack 13y agoI dont think they usually use it as the web server, as they need some scripting not static pages only. The smallest server I know of that can do cgi is webfsd).
- voltagex_ 13y agoHmm, there's another investigation to do.
- mcpherrinm 13y agoWoah, I didn't realize busybox had an httpd. Truly a sweet piece of software.
- voltagex_ 13y agoIt often won't, as features ("applets") are chosen at compile-time.
- gcmalloc 13y agoAdding my two cent: twistd web --path . --port 8080
- aprescott 13y agoThis seems to be straight from Wikipedia and then wrapped in a while loop: https://en.wikipedia.org/wiki/Netcat#Setting_up_a_one-shot_webserver_on_port_8080_to_present_the_content_of_a_file https://en.wikipedia.org/wiki/Netcat#Setting_up_a_one-shot_w... netcat's cool, but the post could've gone a bit further. If it's an intro "how to do this", maybe explain the moving parts for beginners?
- deleted 13y ago[deleted]
- argonaut 13y agonpm install -g serve serve
- raldi 13y agoThat's not a one-line script. That's a multi-line script with the linebreaks removed.
- jlgreco 13y agoI suggest you don't say things like that around Perl programmers.
- AsymetricCom 13y agoMost Perl one-liners don't have semicolons.
- jlgreco 13y agoThe language lends itself to that, but as far as I am aware there is no prejudice against Perl one-liners that do contain semicolons.
- mrhazel 13y agoIt is 85 characters long. Strip some spaces from it, and it qualifies as a one-liner.
- amitamb 13y agoSince nobody mentioned ruby version ruby -run -e httpd . -p 5000
- madlynormal 13y agohttp-server works well if you have node installed. https://npmjs.org/package/httpserver https://npmjs.org/package/httpserver
- theone 13y agoIf you have node-static installed. npm install -g node-static # serve up the current directory $ static serving "." at http://127.0.0.1:8080 More at https://github.com/cloudhead/node-static https://github.com/cloudhead/node-static
- prezjordan 13y agoWith built-in caching to boot!
- adduc 13y agoSimilarly, if PHP 5.4 (or greater) is installed: php -S 127.0.0.1:8080
- DarkStar851 13y agoThis my defacto, PHP is installed on all the Linux servers I manage, mostly Debian/Ubuntu with DotDeb repos. You could probably get nc to pipe the output of php-cli for older PHP installs. http://stackoverflow.com/questions/3940046/can-i-use-netcat-to-run-php-cli-w-arguments http://stackoverflow.com/questions/3940046/can-i-use-netcat-... seems to have a basic example.
- shurcooL 13y agoIn one line, using Go and goe [1]. :) $ goe 'net/http' 'ListenAndServe(":8080", FileServer(Dir("/usr/share/doc/")))' [1] https://github.com/shurcooL/goe https://github.com/shurcooL/goe
- deleted 13y ago[deleted]
- kokey 13y agoI remember trying to write a web server that can respond to requests in bash, several years ago. Since it didn't have sockets support that part was going to be handled by netcat. The problem I ended up with was that bash couldn't read and write to the same pipe. The mkfifo solution was the closest that I got to. It turns out that ksh was perfectly capable of reading and writing to the same pipe at that stage. Not long after I got stuck with bash, awk started to support sockets so I used that. Then bash started to support sockets, and many more features, so I suspect it's possible to write a solution in bash now that doesn't need netcat, but might still need a fifo.
- tga 13y agoSince everyone uses the one line of bash to just start their favourite complex network utility, I'll also contribute mine: while true; sudo /usr/sbin/apachectl start; break; done;
- general_failure 13y agoWhy is there a while true?
- jiggy2011 13y agoI came to this thread to post the exact same joke.
- ja27 13y agoThat's nothing. I made a webserver with just two keystrokes: control-C, control-V. You do have to have Apache installed though.
- tga 13y agoAnd, for the sake of accuracy, the proper bash syntax would be: while true; do sudo /usr/sbin/apachectl start; break; done;
- neeee 13y agoYou can actually write a web server in pure bash, with no netcat, using /dev/tcp (bash magic). https://www.gnu.org/software/bash/manual/html_node/Redirections.html https://www.gnu.org/software/bash/manual/html_node/Redirecti... http://www.linuxjournal.com/content/more-using-bashs-built-devtcp-file-tcpip http://www.linuxjournal.com/content/more-using-bashs-built-d...
- defap 13y agoThat's demonstrating an HTTP client, not a server. I don't think there's a way to make /dev/tcp accept incoming connections.
- zerop 13y agonetcat is the most handy service I have seen on Linux. Any more like this?
- jlgreco 13y agoLook into Socat. Socat is to netcat as Vim is to vi.
- zerop 13y agoNetcat for 21st Century Ncat -- http://nmap.org/ncat/ http://nmap.org/ncat/
- fooyc 13y agoNice. However: $ curl localhost:8080 & curl localhost:8080 <!doctype html> [...] curl: (7) Failed connect to localhost:8080; Connection refused Yes, the server "restarts" after each request, and during the restart, it's not listening. That's not what I would call a "server".
- lambda 13y agoThe socat version I posted https://news.ycombinator.com/item?id=6224060 https://news.ycombinator.com/item?id=6224060 fixes this problem, but is a bit more cumbersome.
- nitin1213 13y agonice
- nitin1213 13y agonice share
- voltagex_ 13y agoI'm sure I've seen an example of using /dev/tcp - a bash builtin.
- lambda 13y agosocat version; no loop, as it forks a separate process for each connection: socat TCP-LISTEN:8080,fork,crnl SYSTEM:'printf \"HTTP/1.1 200 OK\\n\\n\"\; cat test.html' The extra quoting you need to do the the SYSTEM argument makes this rather cumbersome. socat is a powerful tool, but can sometimes be a pain to use. Here's one use I found for it recently, when I wanted to do some experimentation with multicast: socat - UDP-DATAGRAM:239.255.1.1:4242,ip-add-membership=239.255.1.1:10.0.0.10,ip-multicast-loop=0,bind=:4242 This causes socat to bind to the multicast group 239.255.1.1 on the interface that has the unicast IP 10.0.0.10, sending and receiving on port 4242 over UDP, reading from stdin and writing to stdout. It basically gives you a simple chat service over the local network.
- thefox 13y agoOn Debian 7.0 (Wheezy) argument "-l 8080" without "-p" causes netcat (v1.10-40) to listen on a random port. I must run "nc -l -p 8080" to listen at 8080.
- marcelocamanho 13y agoAnyone got a one-line or so version with BASIC auth?
- anonymouz 13y agoI can recommend 'woof' [1] for simple ad-hoc sharing of files through HTTP. It has some extra features, like quickly sharing a directory as tar file. [1] http://www.home.unix-ag.org/simon/woof.html http://www.home.unix-ag.org/simon/woof.html
- deleted 13y ago[deleted]