Page 320 -
P. 320

debugging data


          The data appears to have changed type



                                                                         ...ummm, I see. But
                    Look at the CGI                                      somehow, the data
                    code...it gets the data                              that arrives isn’t an
                    from the model and                                    AthleteList.
                    sends it to the web
                    browser...





















           Let’s add a debugging line of code to your CGI script to try and determine what’s
           going on. Recall that the CGI mechanism captures any output your script sends
           to standard output by default, so let’s use code like this to send your debugging
           messgage to the web server’s console, which is displaying on standard error:  Redirect the output
                                                                                        from “print()” to
          Import “sys”      import sys                                                 “stderr”, rather
           from the                                                                    than the default,
           standard library.   print(json.dumps(athletes[athlete_name]), file=sys.stderr)  which is “stdout”.



           Run your app again and, of course, it’s still crashes with a TypeError.
           However, if you check your web server’s console screen, you’ll see that the
           data being sent as the JSON web response is clearly visible. Notice anything?
                            File  Edit   Window  Help   JustWhatsInTheData
                            $ python3 simple_httpd.py
          This is a list of   Starting simple_httpd on port: 8080
                            192.168.1.33 - - [18/Sep/2010 17:40:04] "GET /cgi-bin/generate_names.py HTTP/1.1" 200 -
           athlete timing   192.168.1.33 - - [18/Sep/2010 17:40:08] "POST /cgi-bin/generate_data.py HTTP/1.1" 200 -
           values…but where’s   ["2-44", "3:01", "2.44", "2.55", "2.51", "2:41", "2:41", "3:00", "2-32", "2.11", "2:26"]
           the name and
           DOB values?






           284    Chapter 8
   315   316   317   318   319   320   321   322   323   324   325