Live data from Hacker News

Web server in one line of bash

razvantudorica.com

51–60 of 69 posts

Re: Web server in one line of bash

#51
post #26

If 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

Or 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 -ou…

I 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

Re: Web server in one line of bash

#52

Earlier quoted context omitted.

Why? It only takes one ^C

For 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.

Indeed. It could use in front of while loop something like:

int_cmd(){exit 1;}trap 'int_cmd' 2;

Re: Web server in one line of bash

#53
post #33

Since 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;

Why is there a while true?

Otherwise the break has nothing to break out of.

Re: Web server in one line of bash

#54
post #33

Since 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;

And, for the sake of accuracy, the proper bash syntax would be:

  while true; do sudo /usr/sbin/apachectl start; break; done;

Re: Web server in one line of bash

#55
post #26

If 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

Or 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 -ou…

erm...chrome speech API?!? What exactly are you doing? and how can I do it too?

Re: Web server in one line of bash

#56

Earlier quoted context omitted.

Thanks! 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.

I 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).

Hmm, there's another investigation to do.

Re: Web server in one line of bash

#57
post #33

Since 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;

I came to this thread to post the exact same joke.

That's nothing. I made a webserver with just two keystrokes: control-C, control-V. You do have to have Apache installed though.

Re: Web server in one line of bash

#59
socat 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.

Re: Web server in one line of bash

#60
post #48

Nice. However: $ curl localhost:8080 & curl localhost:8080 [...] 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".

The socat version I posted https://news.ycombinator.com/item?id=6224060 fixes this problem, but is a bit more cumbersome.
Post reply on HN