Snippets

Ben Buchanan BASH: less-simple script for starting and stopping python SimpleHTTPServer

Created by Ben Buchanan last modified
#!/bin/bash

# Open source, MIT license: Copyright (c) 2017 Ben Buchanan. https://opensource.org/licenses/MIT
# This script provides two commands, "server" and "serverstop"
# Enable by saving server.sh locally, and sourcing it in your .bash_profile:
#
#   source /path/to/server.sh
#
# Checked with https://www.shellcheck.net
# Tested on OSX, WSL and Cygwin.
# Known issue: detection of running server not 100% reliable.
# Inspired by https://gist.github.com/1525217 but has grown a bit over several years

# SERVER
# Set up a simple python server from current directory.
# Usage: 
#   server        # start on default port
#   server 1234   # override default port
function server() { 

  local host
  local port
  local openbrowser
  local machinehost
  local localhost

  host=$(hostname)
  port="${1:-8888}" # take first arg as port number, or default to 8888
  openbrowser=true
  machinehost="http://${host}:${port}"
  localhost="http://localhost:${port}"

  # Check for existing server.
  echo "Checking for existing server at ${localhost} (will try for max 15s)"
  if curl -sI "${localhost}" --connect-timeout 5 --max-time 15 | grep --silent "200 OK" ;then
    echo "Server already running at ${localhost} - choose another port with 'server 1234'"
    return
  else
    echo "Server did not respond within timeout, assuming ${localhost} is available."
  fi

  # The command to open the browser varies between platforms
  case "$(uname -s)" in
    CYGWIN*)
      opencommand="cygstart"
      ;;

    Darwin*)
      opencommand="open"
      ;;

    Linux*)     
      opencommand="xdg-open"

      # check for WSL which initially identifies as Linux
      if grep --silent 'Microsoft' /proc/sys/kernel/osrelease; then
        opencommand="explorer.exe"
      fi
      ;;

    *)
      echo "Browser won't open automatically."
      openbrowser=false
      ;;
  esac

  if [[ $openbrowser ]]; then
    echo "Browser will open automatically"
    (sleep 1 && ${opencommand} "${localhost}")&
  fi

  echo "Starting server."
  echo "Server should be visible at both ${localhost} and http://${host}:${port}/"
  echo "Use ctrl+c to stop server."
  python -m SimpleHTTPServer "$port"

}

# SERVERSTOP
# Kills python server. Useful for hung servers or servers running in background processes
# Usage:
#   serverstop        # kill server without checking port
#   serverstop 1234   # kill server on specific port
function serverstop() {

  local target
  local greptarget
  local grepcount

  if [[ $1 ]];then
    target="SimpleHTTPServer $1"
  else
    target="SimpleHTTPServer"
  fi

  greptarget="[p]ython -m $target"
  grepcount=$(pgrep -f "$greptarget" | wc -l)

  case $grepcount in
    0) 
      echo "No servers running." 
      ;;
    1)
      echo "Stopping $target"
      kill -9 "$(pgrep -f "$greptarget")"
      ;;
    *)
      pgrep "$greptarget" | xargs ps -p
      echo "Multiple matches, specify port: serverstop 1234"
      ;;
  esac

}

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.