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
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'
source ~/.bashrc # or source ~/.zshrc
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