開発環境
- 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 8(Mobile app Development: Small devices)、ANDROID CODE MAGNETS(p.275)を解いてみる。
ANDROID CODE MAGNETS(p.275)
コード(BBEdit)
sample275.py
#!/usr/bin/env python #-*- coding: utf-8 -*- import android import json import time from urllib import urlencode from urllib2 import urlopen hello_msg = "Welcome to Coach Kelly's Timing App" list_title = 'Here is your list of athletes:' quit_msg = "Quitting Coach Kelly's App." web_server = 'http://127.0.0.1:8080' get_names_cgi = '/cgi-bin/generate_names.py' def send_to_server(url, post_data=None): if post_data: page = urlopen(url, urlencode(post_data)) else: page = urlopen(url) return page.read().decode('utf8') def status_update(msg, how_long=2): app.makeToast(msg) time.sleep(how_long) athlete_names = sorted(json.loads(send_to_server(web_server + get_names_cgi))) app = android.Android() status_update(hello_msg) app.dialogCreateAlert(list_title) app.dialogSetSingleChoiceItems(athlete_names) app.dialogSetPositiveButtonText('Select') app.dialogSetNegativeButtonText('Quit') app.dialogShow() resp = app.dialogGetResponse().result status_update(quit_msg)
ブラウザ, Safari(HTML)
http://localhost:8080/cgi-bin/generate_names.py
<html><head></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;"> ["Vera Vi", "James Lee", "Sarah Sweeney", "Sally Sanchez", "Julie Jones", "Mikey McManus"] </pre></body></html>
サーバー
入出力結果(Terminal)
$ ./simple_httpd.py Starting simple_httpd on port: 8080 127.0.0.1 - - [30/Jun/2014 14:25:27] "GET / HTTP/1.1" 200 - 127.0.0.1 - - [30/Jun/2014 14:26:51] "GET /cgi-bin/generate_names.py HTTP/1.1" 200 - 127.0.0.1 - - [30/Jun/2014 14:28:59] "GET /cgi-bin/generate_names.py HTTP/1.1" 200 - C-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 $
クライアント
入出力結果(Terminal)
$ python Python 2.7.5 (default, Mar 9 2014, 22:15:05) [GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import android import json import time from urllib import urlencode from urllib2 import urlopen hello_msg = "Welcome to Coach Kelly's Timing App" list_title = 'Here is your list of athletes:' quit_msg = "Quitting Coach Kelly's App." web_server = 'http://127.0.0.1:8080' get_names_cgi = '/cgi-bin/generate_names.py' def send_to_server(url, post_data=None): if post_data: page = urlopen(url, urlencode(post_data)) else: page = urlopen(url) return page.read().decode('utf8') import android >>> import json >>> import time >>> >>> from urllib import urlencode >>> from urllib2 import urlopen >>> >>> hello_msg = "Welcome to Coach Kelly's Timing App" >>> list_title = 'Here is your list of athletes:' >>> quit_msg = "Quitting Coach Kelly's App." >>> web_server = 'http://127.0.0.1:8080' >>> get_names_cgi = '/cgi-bin/generate_names.py' >>> >>> def send_to_server(url, post_data=None): ... if post_data: ... page = urlopen(url, urlencode(post_data)) ... else: ... page = urlopen(url) ... return page.read().decode('utf8') ... >>> athlete_names = sorted(json.loads(send_to_server(web_server + get_names_cgi))) >>> athlete_names [u'James Lee', u'Julie Jones', u'Mikey McManus', u'Sally Sanchez', u'Sarah Sweeney', u'Vera Vi'] >>> quit() $
0 コメント:
コメントを投稿