Monday, May 4, 2009

How to Share any Folder via a Built-in Web Server?

You’re probably aware that OS X includes the powerful Apache Web server; you enable this server on the Sharing System Preferences panel by activating the Web Sharing service.

In OS X 10.5 (this hint won’t work on OS X 10.4), the bundled version of the Python programming language includes its own simple Web server. Using this simple Web server, you can easily make the contents of any folder on your machine available via a Web browser.

In Terminal, first cd to the folder you’d like to make available via the Web server. For example, cd ~/Pictures to switch to your user’s Pictures folder. Next, you activate Python’s built-in Web server with this command: python -m SimpleHTTPServer (and yes, capitalization counts). Press Return, and you’ll see a message stating Serving HTTP on port 8000 .... The important bit there is the port number (8000).

To access the server from the machine you’re running the Python server on, switch to a Web browser, and enter http://localhost:8000 (replace 8000 with the port you used, if you didn’t use the defaut) in the URL bar. When you press Return, you’ll see a listing of all files in the directory. Depending on what types of files are there, and which browser you’re using, you can then click on files to view them in the browser. In my testing, Firefox works better than Safari, as Safari wants to download file types it doesn’t know how to handle while Firefox will often let you open them with a helper application.

To access the server from another machine on your network, you need to know the server computer’s name. You can find this in the Sharing System Preferences panel, in the aptly-named Computer Name section of the panel. Just below the computer’s actual name, you’ll see a line that begins Computers on your local network can access…, followed by the name they’ll need to use to do so. Typically, this is simply the Computer Name followed by .local—so if the machine’s name is OctoMac, for instance, you can then access the server by entering http://OctoMac.local:8000 in your browser’s URL bar.

For access outside your network, you’ll need to open port 8000 in your router—and be aware that there’s no password protection on this simple server, so anyone could technically look at your files.

When you’re done with the Web server, return to Terminal on the host machine and press Control-C. You’ll probably see a bunch of messages flow by in Terminal, but that’s fine—the server will stop, and you’ll get the Terminal command prompt back. As I stated up front, I’m not sure how much real-world use you’ll get out of this hint, as you can do similar things with file sharing, Back to my Mac, screen sharing, FTP servers, and more…still, I found it interesting, and it’s an amazingly quick way to share a given folder with another machine on a temporary basis.


No comments: