Tuesday, November 16, 2010

Simple server running Python script on OS X

This post is really simple. I went exploring the server that comes with stock OS X today, and these are my notes. What I'm thinking might be interesting is to use the browser as a poor man's GUI. I would have a page that lists available programs, that would link to a form to fill in any changes to the default values and the name of a file with the data. Then we'd run the analysis and write the file to disk, and launch Preview with the graphic.

What I accomplished today: running a Python script using cgi-bin that assembles a page which the server sends back, including data loaded from a file, and the result of a Python call to a datetime.date object.

At first I turned off Airport on my laptop before enabling Web Sharing in Sharing Prefs. That was too restrictive, so later I did as suggested here, I modified the config file:

sudo cp /etc/apache2/httpd.conf /etc/apache2/httpd.conf.orig
Password:
$ ls /etc/apache2
extra httpd.conf.orig mime.types other
httpd.conf magic original users

sudo cp /etc/apache2/httpd.conf ~/Desktop/x.txt


[UPDATE: Discussion on whether 127.0.0.1 can be "spoofed" and what good it would do here, here, here. Basically, no.]


But it wouldn't let me edit it at first! I didn't have the correct permissions.

localhost:Desktop telliott_admin$ ls -al x.txt
-rw-r--r-- 1 root staff 17727 Nov 16 13:11 x.txt
localhost:Desktop telliott_admin$ sudo chmod 661 x.txt
localhost:Desktop telliott_admin$ ls -al x.txt
-rw-rw---x 1 root staff 17727 Nov 16 13:11 x.txt

The config file says:

# Change this to Listen on specific IP addresses as shown below to 
# prevent Apache from glomming onto all bound IP addresses.
#
#Listen 12.34.56.78:80
Listen 80


So I changed that line to Listen 127.0.0.1:80


sudo cp ~/Desktop/x.txt /etc/apache2/httpd.conf


I can point Safari at http://localhost/~telliott_admin/ or http://127.0.0.1/~telliott_admin/.
That's my "personal website" which serves this page: /Users/telliott_admin/Sites/index.html. The page has an image


<img src="images/gradient.jpg" alt=""
height="304" width="800" border="0" />


The image is just where you'd expect it to be in /Users/telliott_admin/Sites/images

The page also has a couple of links

<a href="/manual/">Apache manual</a>
<a href="http://www.apache.org/httpd">Apache</a>


The manual referenced by the link is actually here: /Library/WebServer/share/httpd/manual/index.html



My "computers website" is: http://localhost/

This page just says "It works!" It is here: /Library/WebServer/Documents/index.html.en

So I guess when the URL ends with '/' you load the index.html page

There is also an empty directory /Library/WebServer/CGI-Executables, which we're going to use next.

I made a file called data.txt on my Desktop:


No point without some data.
This is my data.



cp data.txt /Library/WebServer/Documents/data.txt


I made a second file with a Python script, called script.py:

#!/usr/bin/python
import os,time
from datetime import date
today = date.today()

fn = '/Library/WebServer/Documents/data.txt'
#fn = '/Documents/data.txt'
FH = open(fn,'r')
data = FH.read()
FH.close()

s = '''Content-type: text/html

<head>Title</head>
<hr>
<p></p>
<body>xyz
<hr>
<p></p>'''

print s, data,
print '<br>'
print 'and this is my date :)'
print '<br>'
print today
print '</body></html>'



$ cp script.py /Library/WebServer/CGI-Executables/script.py
$ sudo chmod +x /Library/WebServer/CGI-Executables/script.py
Password:
$ ls -al /Library/WebServer/CGI-Executables/script.py
-rwxrwxrwx@ 1 telliott_admin staff 298 Nov 16 13:37 /Library/WebServer/CGI-Executables/script.py


Point browser at: http://localhost/cgi-bin/script.py




When it doesn't work, do this:

$ cat /var/log/apache2/error_log

No comments: