2014年7月12日土曜日

開発環境

Head First Python (Paul Barry(著)、 O'Reilly Media )のChapter 9(Manage Your data: Handling input)、SHARPEN YOUR PENCIL(p.337)を解いてみる。

SHARPEN YOUR PENCIL(p.337)

コード(BBEdit, Emacs)

cgi-gin/generate_list.py

#!/usr/bin/env python3
#-*- coding: utf-8

import athletemodel
import yate

athletes = athletemodel.get_namesID_from_store()

print(yate.start_response())
print(yate.include_header("NUAC's List of Athletes"))
print(yate.start_form("generate_timing_data.py"))
print(yate.para("Select an athlete from the list to work with:"))
for each_athlete in sorted(athletes):
    print(yate.radio_button_id(
        "which_athlete", each_athlete[0], each_athlete[1]))
print(yate.end_form("Select"))
print(yate.include_footer({"Home": "/index.html"}))

cgi-gin/generate_data.py

#!/usr/bin/env python3
#-*- coding: utf-8

import cgi
import json
import athletemodel
import yate

athletes = athletemodel.get_namesID_from_store
form_data = cgi.FieldStorage()
athlete_id = form_data['which_athlete'].value
athlete = athletemodel.get_athlete_from_id(athlete_id)
print(yate.start_response('application/json'))
print(json.dumps(athlete))

cgi-gin/generate_names.py

#!/usr/bin/env python3
#-*- coding: utf-8 -*-

import athletemodel
import yate
import json

athletes = athletemodel.get_namesID_from_store()

print(yate.start_response('application/json'))
print(json.dumps(sorted(athletes)))

cgi-gin/generate_data.py

#!/usr/bin/env python3
#-*- coding: utf-8

import cgi
import json
import athletemodel
import yate

athletes = athletemodel.get_namesID_from_store
form_data = cgi.FieldStorage()
athlete_id = form_data['which_athlete'].value
athlete = athletemodel.get_athlete_from_id(athlete_id)
print(yate.start_response('application/json'))
print(json.dumps(athlete))

cgi-gin/generate_list2.py

#!/usr/bin/env python3
#-*- coding: utf-8

import athletemodel
import yate

athletes = athletemodel.get_namesID_from_store()

print(yate.start_response())
print(yate.include_header("NUAC's List of Athletes"))
print(yate.start_form("generate_data.py"))
print(yate.para("Select an athlete from the list to work with:"))
for each_athlete in sorted(athletes):
    print(yate.radio_button_id(
        "which_athlete", each_athlete[0], each_athlete[1]))
print(yate.end_form("Select"))
print(yate.include_footer({"Home": "/index.html"}))

サーバー

入出力結果(Terminal, IPython)

$ ./simple_httpd.py
Starting simple_httpd on port: 8080
127.0.0.1 - - [12/Jul/2014 17:38:09] "GET /cgi-bin/generate_list.py HTTP/1.1" 200 -
127.0.0.1 - - [12/Jul/2014 17:38:12] "POST /cgi-bin/generate_timing_data.py HTTP/1.1" 200 -
127.0.0.1 - - [12/Jul/2014 17:38:13] "GET /cgi-bin/generate_list.py HTTP/1.1" 200 -
127.0.0.1 - - [12/Jul/2014 17:38:16] "POST /cgi-bin/generate_timing_data.py HTTP/1.1" 200 -
127.0.0.1 - - [12/Jul/2014 17:38:17] "GET /cgi-bin/generate_list.py HTTP/1.1" 200 -
127.0.0.1 - - [12/Jul/2014 17:38:19] "POST /cgi-bin/generate_timing_data.py HTTP/1.1" 200 -
127.0.0.1 - - [12/Jul/2014 17:38:22] "GET /cgi-bin/generate_list.py HTTP/1.1" 200 -
127.0.0.1 - - [12/Jul/2014 17:38:28] "GET /cgi-bin/generate_list2.py HTTP/1.1" 200 -
127.0.0.1 - - [12/Jul/2014 17:38:29] "GET /cgi-bin/generate_list2.py HTTP/1.1" 200 -
127.0.0.1 - - [12/Jul/2014 17:38:33] "POST /cgi-bin/generate_data.py HTTP/1.1" 200 -
127.0.0.1 - - [12/Jul/2014 17:39:02] "GET /cgi-bin/generate_names.py HTTP/1.1" 200 -
127.0.0.1 - - [12/Jul/2014 17:39:02] "GET /cgi-bin/generate_names.py HTTP/1.1" 200 -
  C-c C-cTraceback (most recent call last):
  File "./simple_httpd.py", line 10, in <module>
    httpd.serve_forever()
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/socketserver.py", line 236, in serve_forever
    poll_interval)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/socketserver.py", line 154, in _eintr_retry
    return func(*args)
KeyboardInterrupt
$

0 コメント:

コメントを投稿