Tuesday, November 16, 2010

Simple server running Python script on OS X: part 2


This continues the example from last time, using the built-in server on OS X and trying to understand how to transmit data from a form to a Python script. The form and the script files that I edit are on my Desktop, and after editing I have to remember to do:

cp script.py /Library/WebServer/CGI-Executables/script.py
cp form.html /Library/WebServer/Documents/form.html

I have Web Sharing turned on, with Apache set to only listen to localhost. I point Safari to:

http://localhost/form.html

and I get what's in the graphic. The form text is:

<Content-type: text/html>

<form name="input" action="cgi-bin/script.py" method="get">

First name: <input type="text" name="firstname" /><br />
Last name: <input type="text" name="lastname" /><br /><br />
<input type="radio" name="sex" value="male" /> Male<br />
<input type="radio" name="sex" value="female" /> Female<br /><br />
<input type="checkbox" name="vehicle" value="Bike" /> I have a bike<br />
<input type="checkbox" name="vehicle" value="Car" /> I have a car<br /><br />

<textarea rows="5" cols="40">
The cat was playing in the garden.
</textarea>
<input type="submit" value="Submit" />
</form>
</html>


Actually, in the source for this post I changed all the less-than symbols to < as usual. There has to be a blank line after the first one with Content-type, or I get an error.

The script is very simple. It uses os.environ to get values from the "environment":

#!/usr/bin/python
import os

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

<head></head>'''

print s
D = os.environ
for k in D:
print '<p>', k, D[k], '</p>'
print '</body></html>'

If I type data into the form and then hit submit, the form data goes to the script, which reads it and sends the result back as a web page:

HTTP_REFERER http://localhost/form.html

VERSIONER_PYTHON_PREFER_32_BIT no

SERVER_SOFTWARE Apache/2.2.15 (Unix) mod_ssl/2.2.15 OpenSSL/0.9.8l DAV/2

SCRIPT_NAME /cgi-bin/script.py

SERVER_SIGNATURE

REQUEST_METHOD GET

SERVER_PROTOCOL HTTP/1.1

QUERY_STRING firstname=Tom&lastname=Elliott&sex=male&vehicle=Bike&vehicle=Car

PATH /usr/bin:/bin:/usr/sbin:/sbin

HTTP_USER_AGENT Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_5; en-us) AppleWebKit/533.18.1 (KHTML, like Gecko) Version/5.0.2 Safari/533.18.5

HTTP_CONNECTION keep-alive

SERVER_NAME localhost

REMOTE_ADDR 127.0.0.1

SERVER_PORT 80

SERVER_ADDR 127.0.0.1

DOCUMENT_ROOT /Library/WebServer/Documents

SCRIPT_FILENAME /Library/WebServer/CGI-Executables/script.py

SERVER_ADMIN you@example.com

HTTP_HOST localhost

REQUEST_URI /cgi-bin/script.py?firstname=Tom&lastname=Elliott&sex=male&vehicle=Bike&vehicle=Car

HTTP_ACCEPT application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5

GATEWAY_INTERFACE CGI/1.1

REMOTE_PORT 51027

HTTP_ACCEPT_LANGUAGE en-us

__CF_USER_TEXT_ENCODING 0x46:0:0

VERSIONER_PYTHON_VERSION 2.6

HTTP_ACCEPT_ENCODING gzip, deflate

So, it looks pretty good.

There is a big thing missing, however. I do not yet know how to obtain the data from the textarea. Baby steps.