51

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.

4 Answers 4

78

If you have python installed:

$ python -m SimpleHTTPServer
Serving HTTP on 0.0.0.0 port 8000 ...
Sign up to request clarification or add additional context in comments.

4 Comments

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) ?
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.
I just checked again... apparently Macs do come with python pre-installed, at least mines do have it.
Note that the name has changed under Python 3.
15

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

1 Comment

better than the accepted since SimpleHPPTServer was removed in python3, and moderns Macs come with python3
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).

Comments

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.

4 Comments

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.
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 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.
And "Web Sharing" is only available if you have the server version of the OS

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.