1. Add the following configurations to your lighttpd.conf:
server.modules = ( "mod_cgi" )
cgi.assign = (
".py" => "/usr/bin/python2"
)
2. Write a simple website as an interface to add two integers:
<html>
<head>
<title>Test CGI</title>
</head>
<body>
<form action="/test.py">
<p>Add two integer, please input two integer:</p>
<input name="a" size="4"/>
<input name="b" size="4"/>
<input type="submit" values="submit"/>
</form>
</body>
</html>
3. Write the test.py to add two or more integers:
# Test python for cgi
import os
import urlparse
import cgitb
import string
cgitb.enable()
# Get the query string from environment, it is set by lighttpd
query = os.getenv('QUERY_STRING')
sum = 0
# Print the header
print 'Content-Type: text/html'
print
print '<title>Python CGI</title>'
print '<h1>Test Python for CGI</h1>'
# Sum up all the values
if query is not None:
pairs = urlparse.parse_qs(query) # pairs is a dict
for key, value in pairs.items(): # value is a list
print '<p>key = %s, value = %s</p>' %(key, value)
sum += string.atoi(value[0])
# Print the result
print '<p>Sum = %d</p>' %sum
4. OK, it is very easy, have fun!