5 ms·
My usual go-to for a quick static server is: python -m http.server But variations exist for a lot of languages. Php has one built-in too
by tyzoid 1y ago
My usual go-to for a quick static server is:
python -m http.server
But variations exist for a lot of languages. Php has one built-in too
- zikduruqe 1y agoDon't forget bash. #!/bin/bash while :; do nc -l 80 < index.html; done
- xmodem 1y agoI was about to down-vote you, but that would be unfair, as this has roughly the typical level of correctness of most bash scripts.
- rollcat 1y agoIt's absolutely (almost) correct! HTTP/0.9 does not require you to send back a status code or any headers. Some modern web servers even recognise a lone "GET /" to mean HTTP/0.9 and will respond accordingly.
- xmodem 1y agoThis is exactly my point - it successfully accomplishes a very specific task, in a way that is fragile and context dependent, and completely fails to handle any errors or edge cases, or reckon with any complexity at all.
- vFunct 1y agoThis is hilarious
- creatonez 1y agoFor anyone baffled by this: This works because HTTP/0.9 (just called "HTTP" at the time) worked in an extremely simple way, and browsers mostly retained compatibility for this. HTTP/0.9 web browser sends: GET / Netcat sends: <!doctype html> ... Nowadays a browser will send `GET / HTTP/1.1` and then a bunch of headers, which a true HTTP/0.9 server may be able to filter out and ignore, but of course this script will just send the document and the browser will still assume it's a legacy server.
- blacksmith_tb 1y agoThere are some nice compilations of those, like https://gist.github.com/willurd/5720255 https://gist.github.com/willurd/5720255
- nodesocket 1y agoI use python for serving my local static site development with this custom little bash wrapper script I wrote: #!/usr/bin/env bash set -e; [[ $TRACE ]] && set -x port=8080 dir="." if [[ "$1" == "-h" || "$1" == "--help" ]]; then echo "usage: http-server [PORT] [DIRECTORY]" echo " PORT Port to listen on (default: 8080)" echo " DIRECTORY Directory to serve (default: .)" exit 0 fi if [ -n "$1" ]; then port=$1 fi if [[ -n "$2" ]]; then dir=$2 fi python3 -m http.server --directory "$dir" --protocol HTTP/1.1 "$port"
- Y_Y 1y agoFrom the people who brought you Useless Use of Cat, here's our newest innovation: Useless Use of Bash! That whole script could just be the last line! Maybe you could add defaults like "${port:-8080}"
- nodesocket 1y agoFair, but don't need to be snooty about it. :-) port="${1:-8080}" dir="${2:-.}"
- DiggyJohnson 1y agoGenuinely curious about what the full script would look like in consideration of your feedback.
- porridgeraisin 1y agopython3 -m http.server "${$1:-8080}" "${$2:-.}"
- Y_Y 1y agoAlmost! That will read the variable whose name is is the script argument. Also the directory argument needs a flag on my setup. It should be: python3 -mhttp.server "${1:-8080}" -d "${2:-.}"
- red_admiral 1y agoPython is my go-to method too, altough the config file approach from this project looks exciting. (I'm sure if I dug in the http.server documentation I could find all those options too.)