Page 308 -
P. 308
json-generating cgi script
With your new function written and added to the athletemodel module, you were to create
a new CGI script that, when called, returns the data from the get_names_from_store()
function to the web requester as a JSON data stream.
You were to call your new script cgi-bin/generate_names.py.
Don’t forget this #! /usr/local/bin/python3
“magic” first line
if you’re running
on Linux or Mac
OS X. import json
import athletemodel Do your imports.
import yate
Get the data from
names = athletemodel.get_names_from_store() the model.
Start with the
appropriate
“Content-type”:
line. print(yate.start_response('application/json')) Sort “names”, then convert
print(json.dumps(sorted(names))) to JSON and send to
STDOUT.
Take care testing
your JSON-generating
CGI code.
The behavior you see
when testing your JSON-
generating CGI script will differ
depending on the web browser you
are using. For instance, Firefox might
attempt to download the generated
data as opposed to display it on screen.
272 Chapter 8