2014年7月2日水曜日

開発環境

Head First Python (Paul Barry(著)、 O'Reilly Media )のChapter 8(Mobile app Development: Small devices)、SHARPEN YOUR PENCIL(p.279)を解いてみる。

SHARPEN YOUR PENCIL(p.279)

コード(BBEdit)

sample279.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'
# sharpen your pencil p.279
get_data_cgi = '/cgi-bin/generate_data.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

# sharpen your pencil p.279
if resp['which'] == 'positive':
    i = app.dialogGetSelectedItems().result
    name = athlete_names[i]
    athlete = json.loads(send_to_server(
        web_server + get_data_cgi, {'witch_athlete': name}))
    app.dialogCreateAlert(name)
    app.dialogSetItems(athlete['top3'])
    app.dialogSetPositiveButtonText('Close')
    app.dialogShow()
    
status_update(quit_msg)

サーバー

入出力結果(Terminal, IPython)

$ ./simple_httpd.py
Starting simple_httpd on port: 8080
  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
$

クライアント(Android Emulator)

入出力結果(Terminal)

…省略…
Connection refused

アクセスを許可してるつもりだけど、上手くいかない。どこか設定に問題があるのかなぁ。

5 コメント:

MindtechAffiliatesさんのコメント...

You Provide Best Information. I like it.this blog very interested. Thank you for sharing this useful Article.
Online affiliates

OGEN Infosystem (P) Limitedさんのコメント...

Great and valuable information put in this blog, thank you so much for sharing this information with us. Visit OGEN Infosystem for responsive web design and SEO Service.
SEO Service in Delhi

Raj Maanさんのコメント...

Very valuable post...! This information shared is helpful to improve my knowledge skill. Thank you...! putlocker

匿名さんのコメント...

tik tok shayari. you can simply book c

best quality of SEO Submission sites listさんのコメント...

Right Here is UAE (Dubai) Business Listing Sites List 2020-21, which you can use to obtain top quality backlink for your website as well as enhance your organization in Dubai.

コメントを投稿