Page 338 -
P. 338

add timing data



                           CGI Magnets Solution


                           You need a new CGI script called add_timing_data.py,
                           which processes the data from a form and displays the data on
                           your web server’s console screen. The CGI needs to query the
                           environment, arranging to display the logged data on one line.
                           The code exists, but most of it is all over the floor. You were to
                           rearrange the magnets to produce a working program.



                           #! /usr/local/bin/python3


                           import cgi
                           import os
                           import time
                           import sys
                           import yate


                           print(yate.start_response('text/plain'))
                           addr = os.environ['REMOTE_ADDR']
                           host = os.environ['REMOTE_HOST']
                           method = os.environ['REQUEST_METHOD']
                           cur_time = time.asctime(time.localtime())
                           print(host + ", " + addr + ", " + cur_time + ": " + method + ": ",
                                         end='', file=sys.stderr)            Ensure that this “print()”
                           form = cgi.FieldStorage()                         function does NOT take a
                                                                             newline.
                           for each_form_item in form.keys():


                                    print(each_form_item + '->' + form[each_form_item].value,
                                               end=' ',        file=sys.stderr)
                           print(file=sys.stderr)           T ake a newline on standard error.

                           print('OK.')











           302    Chapter 9
   333   334   335   336   337   338   339   340   341   342   343