Today’s hint may not prove all that useful, as there are many other ways of doing basically the same thing as this hint will demonstrate. Nonetheless, I felt it worth sharing as it demonstrates some of the features hiding in the various Unix programs bundled with OS X.
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. However, that may be overkill for your needs—maybe you just want to temporarily make a folder on your machine available to another machine, either in your home/work or to the world as a whole. If that’s the case, then today’s hint may be the solution you’re looking for.
Read more …
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 0.0.0.0 port 8000 .... The important bit there is the port number (
8000). (You can use a different port number by simply including it at the end of the command, like so:
python -m SimpleHTTPServer 8080.)
To access the server from the machine you’re running the Python server on, switch to a Web browser, and enter
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. (They can’t make changes to anything; this is strictly a view-mode interface.)
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.
I found this tip on commandlinefu.com, a site that collects useful Unix commands. If you’re a Terminal user, there are some gems hiding there, though it may take some digging to find them all.