開発環境
- OS X Mavericks - Apple(OS)
- Emacs (CUI)、BBEdit - Bare Bones Software, Inc. (GUI) (Text Editor)
- Python (プログラミング言語)
Head First Python (Paul Barry(著)、 O'Reilly Media )のChapter 9(Manage Your data: Handling input)、CGI MAGNETS(p.301)を解いてみる。
CGI MAGNETS(p.301)
コード(BBEdit)
cgi-bin/add_timing_data.py
#!/usr/bin/env python3 #-*- coding: utf-8 -*- 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('{0}, {1}, {2}: {3}: '.format(host, addr, cur_time, method), end='', file=sys.stderr) form = cgi.FieldStorage() for each_form_item in form.keys(): print('{0}->{1}'.format(each_form_item, form[each_form_item].value), end=' ', file=sys.stderr) print(file=sys.stderr) print('OK.')
cgi-bin/sample301.py
#!/usr/bin/env python3 #-*- coding: utf-8 -*- import yate print(yate.start_response()) print(yate.do_form('add_timing_data.py', ['time_value'], text='Send'))
サーバー
入出力結果(Terminal, IPython)
$ ./simple_httpd.py Starting simple_httpd on port: 8080 127.0.0.1 - - [05/Jul/2014 15:04:44] "GET /cgi-bin/sample301.py HTTP/1.1" 200 - 127.0.0.1 - - [05/Jul/2014 15:04:47] "POST /cgi-bin/add_timing_data.py HTTP/1.1" 200 - , 127.0.0.1, Sat Jul 5 15:04:47 2014: POST: time_value->12.34 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 コメント:
コメントを投稿