#! /usr/bin/python
import scgi
import scgi.scgi_server
import time

def print_time(outfile):
    # HTTP header describing the page we're about to produce.
    # Must end with double MS-DOS-style "CR/LF" end-of-line sequence.
    # In Python, that translates to "\r\n.
    outfile.write("Content-Type: text/plain\r\n\r\n")
    # Now write our page: the time, in plain text
    outfile.write(time.ctime() + "\n")

class TimeHandler(scgi.scgi_server.SCGIHandler):
    def produce(self, env, bodysize, input, output):
        # Read arguments
#        argstring = env['QUERY_STRING']
        # Break argument string into list of pairs like "name=value"
#        arglist = argstring.split('&')

        # Set up dictionary mapping argument names to values
#        args = {}
#        for arg in arglist:
#          (key, value) = arg.split('=')
#          args[key] = value

        # Print time, as before, but with a bit of extra advice
        print_time(output)
#        output.write(
#         "Time for a pizza. I'll have the %s and a swig of %s!\n" %
#	(args['pizza'], args['drink'])
#        )
# this wanted to be called as:
# http://crystalfaeries.net/scgi?pizza=hawaii&drink=beer

# Main program: create an SCGIServer object to
# listen on port 4000.  We tell the SCGIServer the
# handler class that implements our application.
server = scgi.scgi_server.SCGIServer(
    handler_class=TimeHandler,
    port=4000
    )
# Tell our SCGIServer to start servicing requests.
# This loops forever.
server.serve()

# http://crystalfaeries.net/linux/bin/scgi
