Skip to content

Instantly share code, notes, and snippets.

@tngranados
Last active February 4, 2020 16:17
Show Gist options
  • Save tngranados/41e63c5bfb2eaa371750805b397d6dcb to your computer and use it in GitHub Desktop.
Save tngranados/41e63c5bfb2eaa371750805b397d6dcb to your computer and use it in GitHub Desktop.
ZSH static server
# Creates a static server using ruby, php, or python 2 or 3, whichever is
# available. It support an optional port (default is 8000).
shttp() {
local port="${1:-8000}"
if (( $+commands[ruby] )); then
ruby -run -ehttpd . -p$port
elif (( $+commands[php] )); then
php -S localhost:$port
elif (( $+commands[python] )); then
local pythonVer=$(python -c 'import platform; major, _, _ = platform.python_version_tuple(); print(major);')
if [ $pythonVer -eq 2 ]; then
python -m SimpleHTTPServer $port
else
python -m http.server $port
fi
else
echo "Error: Ruby, PHP or Python needed"
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment