Skip to content

Instantly share code, notes, and snippets.

@yetimdasturchi
Last active March 29, 2025 23:30
Show Gist options
  • Save yetimdasturchi/4ad7850e5e47a9498c9d61e56e7c890c to your computer and use it in GitHub Desktop.
Save yetimdasturchi/4ad7850e5e47a9498c9d61e56e7c890c to your computer and use it in GitHub Desktop.
Single line web server: bash aliases to quickly spin up a Python HTTP server.

Single line web server

Bash aliases to quickly spin up a Python HTTP server:

  • Starts at a default or custom port
  • Automatically finds the next available port if taken
  • Optionally opens the server in your default web browser

Installation

Add this to your .bashrc, .zshrc or shell profile:

alias serv='function _serv(){ p=${1:-8000}; while lsof -i:$p >/dev/null 2>&1; do ((p++)); done; python3 -m http.server $p; }; _serv'
alias oserv='function _serv(){ p=${1:-8000}; while lsof -i:$p >/dev/null 2>&1; do ((p++)); done; (sleep 1 && (command -v xdg-open >/dev/null && xdg-open http://localhost:$p || open http://localhost:$p)) & python3 -m http.server $p; }; _serv'

Update source commands

source ~/.bashrc # or source ~/.zshrc

Example:

serv        # Start server on default port (8000) or next free one
serv 9090   # Start server on custom port (9090)
oserv       # Start server and open in default browser
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment