49

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.

philippbosch
  • 761
  • 5
  • 9

4 Answers4

78

If you have python installed:

$ python -m SimpleHTTPServer
Serving HTTP on 0.0.0.0 port 8000 ...
Mike Axiak
  • 11,827
  • 2
  • 33
  • 49
  • 1
    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) ? – Arc Sep 26 '10 at 22:58
  • 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 Sep 26 '10 at 23:04
  • 1
    I just checked again... apparently Macs do come with python pre-installed, at least mines do have it. – Arc Sep 28 '10 at 00:04
  • 1
    Note that the name has [changed](http://stackoverflow.com/questions/7943751/what-is-the-python3-equivalent-of-python-m-simplehttpserver) under Python 3. – Kyle Krull May 23 '12 at 18:31
9

Python3 can serve the current directory via HTTP using http.server:

$ python3 -m http.server

Where

  • python3 the current version of python
  • -m stands for module
  • http the http package
  • http.server the server module (of the http package)

Per default, http.server listens on port 8000, but you can specify another like this:

$ python3 -m http.server 8080
Bengt
  • 14,011
  • 7
  • 48
  • 66
6

$ 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).

PhilR
  • 5,375
  • 1
  • 21
  • 27
3

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.

Community
  • 1
  • 1
JBRWilkinson
  • 4,821
  • 1
  • 24
  • 36
  • 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 Sep 29 '10 at 10:45
  • Define "simplest". For me, while I'm already in the dir I'd like to serve out of, executing the aforementioned python line is much simpler than heading into OS X System Preferences, Sharing,… – dmkc Aug 20 '12 at 20:56
  • @dmkc To play devil's advocate, apache is launched by `launchctl` at boot and so you set it once and its always up. No need for a command or a blocked terminal window. – srquinn Nov 15 '13 at 18:22
  • And "Web Sharing" is only available if you have the server version of the OS – JESii Jul 31 '14 at 19:28