Serve files quickly using built-in python Web-Server in OS X

Posted by: gdelmatto  :  Category: Networking, OS X

Mac OS X (since 10.5) sports a python environment in it’s default install, which includes a very simple HTTP server.
This may come in handy if you want to serve some files quickly over HTTP, without the need to fireup the full-blown Apache available through ‘Web Sharing Service’.
Don’t believe me? Read on to see how you’ll be up in running in just a few seconds.

To launch the simple HTTP server, start a Terminal first, then change to the directory containing the files you want to serve, e.g. your Downloads folder:


$ cd ~/Downloads

Now fire up the web server like this:


$ python -m SimpleHTTPServer
Serving HTTP on 0.0.0.0 port 8000 ...

This will bind to port 8000 by default. To use another port, just give the TCP port as an additional argument on the command line:


$ python -m SimpleHTTPServer 8100
Serving HTTP on 0.0.0.0 port 8100 ...

To access the server, open the link as http://Your-Macs-IP-Address:8000 (or http://Your-Macs-IP-Address:8100 for the second example).
This will provide you with a simple file list to browse and access your files.

You won’t be able to bind to the HTTP default port 80 with regurlar user accounts however. This will only yield a “socket: [Errno 13] permission denied” error message.

To bind to port 80, you need to run the command through ‘sudo’ like this:


$ sudo python -m SimpleHTTPServer 80
Password:
Serving HTTP on 0.0.0.0 port 80 ...

The server will stay in foreground of the Terminal and keep running until you hit CTRL-C.

If you want to know why I needed this, then read on my efforts on upgrading Squeezebox Radio firmare.

Comments are closed.