Live data from Hacker News

Web server in one line of bash

razvantudorica.com

41–50 of 69 posts

Re: Web server in one line of bash

#41

Neat trick, but probably too limited to be of much use. Also, stopping it is kind of a nuisance.

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.

Re: Web server in one line of bash

#42

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 if you have PHP, there is an equivalent in 5.4 and above.

http://php.net/manual/en/features.commandline.webserver.php

Re: Web server in one line of bash

#43
post #11

We've used a similar trick to copy files between coworkers: on server: while true; nc -l $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.

Is your while loop missing a "do" statement, or am I missing something?

Re: Web server in one line of bash

#44
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…

Or:

    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.

Re: Web server in one line of bash

#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".

Post reply on HN