Friday, December 12, 2008

Running Python Script on the Mac Apache Web Server

The Mac OS X operating system has build in Apache Web Server and libraries to run python script. So user does not need to install any extra software to run python script on apache web server.

Open System Preference -> Sharing window and enable 'Web Sharing' option. It starts Apache web server on your machine. Wow so easy !!!


The default location to place CGI programs for the OS X apache installation is in the following directory:

/Library/WebServer/CGI-Executables

Files placed in this directory can be accessed via the following URL:

http://localhost/cgi-bin/script-name

Lets create a simple python script named hello.py under /Library/WebServer/CGI-Executables directory.

#!/usr/bin/python
print "Content-type: text/html\n\n"
print "<html>"
print "<head>"
print "<title>hello</title>"
print "</head>"
print "<body>Test Page</body>"
print "</html>"

Set the executable permission to the python script (chmod 755 hello.py). Now access this script via http://localhost/cgi-bin/hello.py

If webserver throws some error, please look at the log for detailed information. Logs are stored in /var/log/apache2/error_log file. NOTE: location may differ based on the version of OS and version of apache server.


No comments: