views:

51

answers:

3

Is there such a thing as a tiny little webserver that I can invoke from the command line that just fetches files from the local filesystem and serves them via HTTP on specific port?

I'd like to be able to do something like this:

$ cd ~/Sites/mysite
$ serve . 10.0.1.1 8080

This should fire up a webserver that listens on 10.0.1.1:8080 and serves files from the current directory (".") – no PHP, ASP or any of that needed.

Any suggestion greatly appreciated.

+3  A: 

If you have python installed:

$ python -m SimpleHTTPServer
Serving HTTP on 0.0.0.0 port 8000 ...
Mike Axiak
I'm not sure, but is python not part of the core system, i.e. always installed (well, at least for Mac OS X 10.5+ I think, maybe even earlier) ?
Archimedix
Perfect! Thanks a lot!
philippbosch
It is not, but he didn't say it had to be part of the core system. Also, many linux's come with python installed where this command will work.
Mike Axiak
I just checked again... apparently Macs do come with python pre-installed, at least mines do have it.
Archimedix
+1  A: 

$ python -m SimpleHTTPServer [port]

will start a webserver in the current directory serving whatever files are found there.

In a few cases this won't work well, for example the server is single-threaded (so no simultaneous downloads) and doesn't handle byte-range requests (clients expecting Range: support often fail badly).

Phil
+1  A: 

Apache HTTPD is built into Mac OS X - just switch on 'Web Sharing' in the Sharing Preferences.

To make it also work over port 8080, you'd need to add some configuration. See this article on Serverfault for starting point.

JBRWilkinson
Apache may be 'heavier' than the OP was looking for, but utterly the simplest way to do it using built-in tools, especially if your files are under the ~/Sites path.
JulesLt