Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Instant Server for Current Directory
alias server='open http://localhost:8000 && python -m SimpleHTTPServer'
@iammerrick

This comment has been minimized.

Show comment
Hide comment
@iammerrick

iammerrick Dec 27, 2011

npm install serve -g

This offers a serve command with additional configuration like:

serve -p 3000

npm install serve -g

This offers a serve command with additional configuration like:

serve -p 3000

@padolsey

This comment has been minimized.

Show comment
Hide comment
@padolsey

padolsey Dec 27, 2011

You can specify the port number too:

# put this in ~/.profile or whatever u use
server() {
  open "http://localhost:${1}" && python -m SimpleHTTPServer $1
}

E.g.

$ server 8080

You can specify the port number too:

# put this in ~/.profile or whatever u use
server() {
  open "http://localhost:${1}" && python -m SimpleHTTPServer $1
}

E.g.

$ server 8080
@iammerrick

This comment has been minimized.

Show comment
Hide comment
@iammerrick

iammerrick Dec 27, 2011

Thats hot. Does SimpleHTTPServer ship with OSX 10.7?

Thats hot. Does SimpleHTTPServer ship with OSX 10.7?

@padolsey

This comment has been minimized.

Show comment
Hide comment
@padolsey

padolsey Dec 27, 2011

@iammerrick, AFAIK, yes. I'm using a relatively fresh install of Lion and it works fine.

@iammerrick, AFAIK, yes. I'm using a relatively fresh install of Lion and it works fine.

@paulirish

This comment has been minimized.

Show comment
Hide comment
@paulirish

paulirish Dec 28, 2011

ian, yeah it's definitely been in snow leopard since the beginning to it's easy to assume all mac-based developers have it.

ian, yeah it's definitely been in snow leopard since the beginning to it's easy to assume all mac-based developers have it.

@cowboy

This comment has been minimized.

Show comment
Hide comment
@cowboy

cowboy Dec 28, 2011

I've gone slightly further with my serve bash script. It accepts both an optional port, file, and opens a browser for you.

cowboy commented Dec 28, 2011

I've gone slightly further with my serve bash script. It accepts both an optional port, file, and opens a browser for you.

@kitcambridge

This comment has been minimized.

Show comment
Hide comment
@kitcambridge

kitcambridge Dec 28, 2011

servedir may be of interest, too.

servedir may be of interest, too.

@igorw

This comment has been minimized.

Show comment
Hide comment
@igorw

igorw Dec 28, 2011

With PHP 5.4 you can do:

$ php -Slocalhost:8080

Note: Does not support directory listing.

igorw commented Dec 28, 2011

With PHP 5.4 you can do:

$ php -Slocalhost:8080

Note: Does not support directory listing.

@jrgifford

This comment has been minimized.

Show comment
Hide comment
@jrgifford

jrgifford Dec 28, 2011

Not just Mac-based developers. All linux guys have it by default as well. Very handy shortcut to have all around.

Not just Mac-based developers. All linux guys have it by default as well. Very handy shortcut to have all around.

@EmilStenstrom

This comment has been minimized.

Show comment
Hide comment
@EmilStenstrom

EmilStenstrom Dec 28, 2011

SimpleHTTPServer has shipped with Python since forever, so it's cross platform. It takes command line arguments too, so you can specify a port number as the last argument if you like.

http://docs.python.org/library/simplehttpserver.html

SimpleHTTPServer has shipped with Python since forever, so it's cross platform. It takes command line arguments too, so you can specify a port number as the last argument if you like.

http://docs.python.org/library/simplehttpserver.html

@originell

This comment has been minimized.

Show comment
Hide comment
@originell

originell Dec 28, 2011

@iammerrick @padolsey @paulirish Python's SimpleHTTPServer module exists for a long time now (afair 2.0), so there should be no problem with most *nix based systems that are post-2000 :-)

edit:// haha @EmilStenstrom you were faster!

In Python 3, SimpleHTTPServer has been merged into http.server. So:

python -m http.server

it has to be there.

Furthermore many people do not know that the same works with a simple SMTP server (read: email):

python -m smtpd -n -c DebuggingServer localhost:port

DebuggingServer outputs all mail to the console. It's nice if you need to test some forms or mail code 👍

@iammerrick @padolsey @paulirish Python's SimpleHTTPServer module exists for a long time now (afair 2.0), so there should be no problem with most *nix based systems that are post-2000 :-)

edit:// haha @EmilStenstrom you were faster!

In Python 3, SimpleHTTPServer has been merged into http.server. So:

python -m http.server

it has to be there.

Furthermore many people do not know that the same works with a simple SMTP server (read: email):

python -m smtpd -n -c DebuggingServer localhost:port

DebuggingServer outputs all mail to the console. It's nice if you need to test some forms or mail code 👍

@fnichol

This comment has been minimized.

Show comment
Hide comment
@fnichol

fnichol Dec 28, 2011

You can also add a default bind port like so:

##
# Quickly starts a webserver from the current directory.
#
# Thanks to:
# http://superuser.com/questions/52483/terminal-tips-and-tricks-for-mac-os-x
#
# @param [optional, Integer] bind port number, default 8000
web_serve() {
  $(which python) -m SimpleHTTPServer ${1:-8000}
}

(source: https://github.com/fnichol/bashrc/blob/master/bashrc#L874-883)

fnichol commented Dec 28, 2011

You can also add a default bind port like so:

##
# Quickly starts a webserver from the current directory.
#
# Thanks to:
# http://superuser.com/questions/52483/terminal-tips-and-tricks-for-mac-os-x
#
# @param [optional, Integer] bind port number, default 8000
web_serve() {
  $(which python) -m SimpleHTTPServer ${1:-8000}
}

(source: https://github.com/fnichol/bashrc/blob/master/bashrc#L874-883)

@charlesroper

This comment has been minimized.

Show comment
Hide comment
@charlesroper

charlesroper Dec 29, 2011

If you like a bit of Ruby action, especially if you're doing Sass and Compass, this might be of interest: http://get-serve.com/

If you like a bit of Ruby action, especially if you're doing Sass and Compass, this might be of interest: http://get-serve.com/

@passcod

This comment has been minimized.

Show comment
Hide comment
@passcod

passcod Dec 29, 2011

And as you add features, etc, you get mongoose. Okay, so it is another sub-100k binary, and it's not using either Python or Ruby, but it reads htpasswd files and run CGI. What else do you need? :)

passcod commented Dec 29, 2011

And as you add features, etc, you get mongoose. Okay, so it is another sub-100k binary, and it's not using either Python or Ruby, but it reads htpasswd files and run CGI. What else do you need? :)

@mythmon

This comment has been minimized.

Show comment
Hide comment
@mythmon

mythmon Dec 29, 2011

I do a lot of Twisted stuff, so my version looks like this:

alias server="twistd -n web --path ."

mythmon commented Dec 29, 2011

I do a lot of Twisted stuff, so my version looks like this:

alias server="twistd -n web --path ."
@fleas

This comment has been minimized.

Show comment
Hide comment
@fleas

fleas Dec 30, 2011

no python needed, here's the good ole emergency web server.

while true; do nc -l 127.0.0.1 8889 < foo.html; done

or

 nc -l 127.0.0.1 8889 < foo.html

fleas commented Dec 30, 2011

no python needed, here's the good ole emergency web server.

while true; do nc -l 127.0.0.1 8889 < foo.html; done

or

 nc -l 127.0.0.1 8889 < foo.html
@mathiasbynens

This comment has been minimized.

Show comment
Hide comment
@mathiasbynens

mathiasbynens Dec 31, 2011

Inspired by @padolsey’s snippet, I’ve replaced the server alias in my dotfiles repository with the following function:

# Start an HTTP server from a directory, optionally specifying the port
function server() {
    local port="${1:-8000}"
    open "http://localhost:${port}/" && python -m SimpleHTTPServer "$port"
}

Inspired by @padolsey’s snippet, I’ve replaced the server alias in my dotfiles repository with the following function:

# Start an HTTP server from a directory, optionally specifying the port
function server() {
    local port="${1:-8000}"
    open "http://localhost:${port}/" && python -m SimpleHTTPServer "$port"
}
@balupton

This comment has been minimized.

Show comment
Hide comment
@balupton

balupton Jan 1, 2012

For a node.js version, made this a while ago: https://github.com/balupton/simple-server

balupton commented Jan 1, 2012

For a node.js version, made this a while ago: https://github.com/balupton/simple-server

@spudbean

This comment has been minimized.

Show comment
Hide comment
@spudbean

spudbean Jan 2, 2012

My version: defaults the port, but allows you to override it. And opens the browser with your real hostname, instead of localhost, for pasting to other people.

function server() { # via https://gist.github.com/1525217
    local host=`hostname`
    local port="${1:-8888}"
    (sleep 1 && open "http://${host}:${port}/")&
    python -m SimpleHTTPServer "$port"
}

spudbean commented Jan 2, 2012

My version: defaults the port, but allows you to override it. And opens the browser with your real hostname, instead of localhost, for pasting to other people.

function server() { # via https://gist.github.com/1525217
    local host=`hostname`
    local port="${1:-8888}"
    (sleep 1 && open "http://${host}:${port}/")&
    python -m SimpleHTTPServer "$port"
}
@zeffii

This comment has been minimized.

Show comment
Hide comment
@zeffii

zeffii Jul 19, 2012

Thanks guys, from ubuntu I now do this:

function server() {
    local port="${1:-8000}"
    gnome-open "http://localhost:${port}/"
    python -m SimpleHTTPServer "$port"
}

zeffii commented Jul 19, 2012

Thanks guys, from ubuntu I now do this:

function server() {
    local port="${1:-8000}"
    gnome-open "http://localhost:${port}/"
    python -m SimpleHTTPServer "$port"
}
@dweidner

This comment has been minimized.

Show comment
Hide comment
@dweidner

dweidner Sep 3, 2014

Overcomplicated?

function server() {
    local port="${1:-8080}"
    local phpfiles=$(find . -type f -iname "*.php" | awk 'END {print NR;}')
    local phpversion=$(php -v | grep "PHP 5" | sed 's/.*PHP \([^-]*\).*/\1/' | cut -d\  -f 1)
    local phpvmajor=$(echo ${phpversion} | cut -d. -f 1)
    local phpvminor=$(echo ${phpversion} | cut -d. -f 2)

    open "http://localhost:${port}/"

    if [ ${phpvmajor} -ge 5 -a ${phpvminor} -ge 4 -a ${phpfiles} -gt 0 ]; then
        php -S localhost:${port}
    else
        python -m SimpleHTTPServer "${port}"
    fi
}

dweidner commented Sep 3, 2014

Overcomplicated?

function server() {
    local port="${1:-8080}"
    local phpfiles=$(find . -type f -iname "*.php" | awk 'END {print NR;}')
    local phpversion=$(php -v | grep "PHP 5" | sed 's/.*PHP \([^-]*\).*/\1/' | cut -d\  -f 1)
    local phpvmajor=$(echo ${phpversion} | cut -d. -f 1)
    local phpvminor=$(echo ${phpversion} | cut -d. -f 2)

    open "http://localhost:${port}/"

    if [ ${phpvmajor} -ge 5 -a ${phpvminor} -ge 4 -a ${phpfiles} -gt 0 ]; then
        php -S localhost:${port}
    else
        python -m SimpleHTTPServer "${port}"
    fi
}
@ahmedelgabri

This comment has been minimized.

Show comment
Hide comment
@ahmedelgabri

ahmedelgabri Sep 23, 2014

This supports simple native servers for Ruby, Sinatra, PHP & fallback to Python SimpleHTTPServer as default

server() {
    local port="${2:-8000}"
    open "http://localhost:${port}/"

    if [[ "$1" == "ruby" ]]; then
        ruby -run -ehttpd . -p$port
    elif [[ "$1" == "sinatra" ]]; then
        ruby -rsinatra -e'set :public_folder, "."; set :port, $port'
    elif [[ "$1" == "php" ]]; then
        php -S localhost:$port
    else
        # Set the default Content-Type to `text/plain` instead of `application/octet-stream`
        # And serve everything as UTF-8 (although not technically correct, this doesn’t break anything for binary files)
        python -c $'import SimpleHTTPServer;\nmap = SimpleHTTPServer.SimpleHTTPRequestHandler.extensions_map;\nmap[""] = "text/plain";\nfor key, value in map.items():\n\tmap[key] = value + ";charset=UTF-8";\nSimpleHTTPServer.test();' "$port"
    fi
}

This supports simple native servers for Ruby, Sinatra, PHP & fallback to Python SimpleHTTPServer as default

server() {
    local port="${2:-8000}"
    open "http://localhost:${port}/"

    if [[ "$1" == "ruby" ]]; then
        ruby -run -ehttpd . -p$port
    elif [[ "$1" == "sinatra" ]]; then
        ruby -rsinatra -e'set :public_folder, "."; set :port, $port'
    elif [[ "$1" == "php" ]]; then
        php -S localhost:$port
    else
        # Set the default Content-Type to `text/plain` instead of `application/octet-stream`
        # And serve everything as UTF-8 (although not technically correct, this doesn’t break anything for binary files)
        python -c $'import SimpleHTTPServer;\nmap = SimpleHTTPServer.SimpleHTTPRequestHandler.extensions_map;\nmap[""] = "text/plain";\nfor key, value in map.items():\n\tmap[key] = value + ";charset=UTF-8";\nSimpleHTTPServer.test();' "$port"
    fi
}
@wenerme

This comment has been minimized.

Show comment
Hide comment
@wenerme

wenerme May 21, 2015

My universal version, works with npm, python, php and can run under windows, linux and mac

# Instant Server for Current Directory
# https://gist.github.com/JeffreyWay/1525217
function server()
{
    local port=${1:-8000}
    iscmd python && {
        (sleep 1 && o "http://localhost:${port}/")&
        python -m SimpleHTTPServer ${port}
    }

    iscmd npm && (npm -g ls --depth=0 | grep server@) >/dev/null && {
        # Use npm server
        (sleep 1 && o "http://localhost:${port}/")&
        server ${port}
    }

    iscmd php && {
        (sleep 1 && o "http://localhost:${port}/")&
        php -S localhost:${port}
    }
}

Also in my dotfiles. the iscmd is defined in my dotfiles too.

wenerme commented May 21, 2015

My universal version, works with npm, python, php and can run under windows, linux and mac

# Instant Server for Current Directory
# https://gist.github.com/JeffreyWay/1525217
function server()
{
    local port=${1:-8000}
    iscmd python && {
        (sleep 1 && o "http://localhost:${port}/")&
        python -m SimpleHTTPServer ${port}
    }

    iscmd npm && (npm -g ls --depth=0 | grep server@) >/dev/null && {
        # Use npm server
        (sleep 1 && o "http://localhost:${port}/")&
        server ${port}
    }

    iscmd php && {
        (sleep 1 && o "http://localhost:${port}/")&
        php -S localhost:${port}
    }
}

Also in my dotfiles. the iscmd is defined in my dotfiles too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment