Simple Server

Seattle, Washington 2012-06-07

Description

Most Mac-using web developers probably know by now that an awesome way to test work locally is with Python’s SimpleHTTPServer module, which launches a lightweight web server in the current directory.

python -m SimpleHTTPServer

Services Menu

I wanted to take this a step further by adding a custom service that allows users to right-click any directory in Finder and launch both a server and a browser pointing to it. I did this by creating a Service in Automator with a custom AppleScript action.

on run {input, parameters}
    set serverPath to POSIX path of input
    set serverPort to do shell script "PORT=8000; while lsof -i :$PORT > /dev/null; do ((PORT++)) ; done ; echo $PORT"  
    tell application "Terminal" to do script "cd '" & serverPath & "' && python -m SimpleHTTPServer " & serverPort
    delay 0.5   
    do shell script "open https://localhost:" & serverPort
    return input
end run

Download

The complete service is available for immediate use or for editing in within Automator.

Simple HTTP Server Automator Service