public
Last active

Instant Server for Current Directory

  • Download Gist
gistfile1.txt
1
alias server='open http://localhost:8000 && python -m SimpleHTTPServer'

npm install serve -g

This offers a serve command with additional configuration like:

serve -p 3000

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

Thats hot. Does SimpleHTTPServer ship with OSX 10.7?

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

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

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

servedir may be of interest, too.

With PHP 5.4 you can do:

$ php -Slocalhost:8080

Note: Does not support directory listing.

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

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

@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 :+1:

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)

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/

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? :)

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

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

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

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"
}

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

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"
}

Thanks guys, from ubuntu I now do this:

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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.