今さらPython3 (78) - 第11章復習課題

第11章の復習課題。

入門 Python 3

入門 Python 3

11.1

$ python3 udp_time_server.py
Starting the server at 2015-12-28 16:35:54.895217
Waiting for a client to call...
Server sent b'2015-12-28 16:38:32.315371'
Server sent b'2015-12-28 16:38:35.318527'
Server sent b'2015-12-28 16:38:38.319827'
Server sent b'2015-12-28 16:38:41.325652'
Server sent b'2015-12-28 16:38:44.330172'
Server sent b'2015-12-28 16:38:47.336078'
Server sent b'2015-12-28 16:38:50.338064'
Server sent b'2015-12-28 16:38:53.344032'
Server sent b'2015-12-28 16:38:56.348398'
Server sent b'2015-12-28 16:38:59.352635'
$ python3 udp_time_client.py
Starting the client at 2015-12-28 16:38:29.310426
Client read b'2015-12-28 16:38:32.315371'
Client read b'2015-12-28 16:38:35.318527'
Client read b'2015-12-28 16:38:38.319827'
Client read b'2015-12-28 16:38:41.325652'
Client read b'2015-12-28 16:38:44.330172'
Client read b'2015-12-28 16:38:47.336078'
Client read b'2015-12-28 16:38:50.338064'
Client read b'2015-12-28 16:38:53.344032'
Client read b'2015-12-28 16:38:56.348398'
Client read b'2015-12-28 16:38:59.352635'

時間を返すだけで良いなら、クライアント側はWhile使ってループさせなくても良いはずだけどね。

11.2


これじゃ、ほぼ模範解答と同じだね。

サーバー側

$ python3 zmq_time_server.py
Server started at 2015-12-28 18:15:08.691349
Sever sent 2015-12-28 18:15:18.290900
Sever sent 2015-12-28 18:15:21.296744
Sever sent 2015-12-28 18:15:24.299438
Sever sent 2015-12-28 18:15:27.300028
Sever sent 2015-12-28 18:15:30.301898
Sever sent 2015-12-28 18:15:33.307522

クライアント側

$ python3 zmq_time_client.py
Client started at 2015-12-28 18:15:15.289806
Client received b'2015-12-28 18:15:18.290900'
Client received b'2015-12-28 18:15:21.296744'
Client received b'2015-12-28 18:15:24.299438'
Client received b'2015-12-28 18:15:27.300028'
Client received b'2015-12-28 18:15:30.301898'
Client received b'2015-12-28 18:15:33.307522'
11.3

サーバー側。

$ python3 xmlrpc_time_server.py
Server sent %s 2015-12-28 18:28:27.235630
127.0.0.1 - - [28/Dec/2015 18:28:27] "POST / HTTP/1.1" 200 -
Server sent %s 2015-12-28 18:28:30.240954
127.0.0.1 - - [28/Dec/2015 18:28:30] "POST / HTTP/1.1" 200 -
Server sent %s 2015-12-28 18:28:33.245577
127.0.0.1 - - [28/Dec/2015 18:28:33] "POST / HTTP/1.1" 200 -
Server sent %s 2015-12-28 18:28:36.248658
127.0.0.1 - - [28/Dec/2015 18:28:36] "POST / HTTP/1.1" 200 -
Server sent %s 2015-12-28 18:28:39.252739
127.0.0.1 - - [28/Dec/2015 18:28:39] "POST / HTTP/1.1" 200 -

クライアント側。

$ python3 xmlrpc_time_client.py
Current time is 2015-12-28 18:28:27.235630
Current time is 2015-12-28 18:28:30.240954
Current time is 2015-12-28 18:28:33.245577
Current time is 2015-12-28 18:28:36.248658
Current time is 2015-12-28 18:28:39.252739
11.4

なんか急に難易度上がってないか?Redisで皿洗いのイメージかな?

まず、チョコレートを供給する側から起動して、

$ python3 redis_choco_supply.py

ルーシーがもたついている間に、残りがすごいことになってるw。

$ python3 redis_lucy.py
Lucy is ready...
Lucy got a b'milk' at 2015-12-28 18:57:14.396594 . Still remaining 45 pieces
Lucy got a b'green tea' at 2015-12-28 18:57:14.898113 . Still remaining 44 pieces
Lucy got a b'sweet' at 2015-12-28 18:57:15.400273 . Still remaining 44 pieces
Lucy got a b'bitter' at 2015-12-28 18:57:15.902071 . Still remaining 45 pieces
Lucy got a b'milk' at 2015-12-28 18:57:16.403994 . Still remaining 46 pieces
Lucy got a b'bitter' at 2015-12-28 18:57:16.905485 . Still remaining 46 pieces
Lucy got a b'white' at 2015-12-28 18:57:17.409046 . Still remaining 47 pieces
Lucy got a b'milk' at 2015-12-28 18:57:17.910714 . Still remaining 46 pieces
Lucy got a b'sweet' at 2015-12-28 18:57:18.412954 . Still remaining 46 pieces
Lucy got a b'sweet' at 2015-12-28 18:57:18.915009 . Still remaining 46 pieces
Lucy got a b'green tea' at 2015-12-28 18:57:19.416202 . Still remaining 45 pieces
Lucy got a b'bitter' at 2015-12-28 18:57:19.918128 . Still remaining 45 pieces
Lucy got a b'milk' at 2015-12-28 18:57:20.420344 . Still remaining 45 pieces
Lucy got a b'bitter' at 2015-12-28 18:57:20.922142 . Still remaining 45 pieces
Lucy got a b'milk' at 2015-12-28 18:57:21.423829 . Still remaining 45 pieces
11.5

ZeroMQでパブサブ作る感じ?まず7章の練習問題の詩の単語を1つずつパブリッシュする準備として、テキストファイルを準備しておいた方が良いと予想。mammoth.txtとして保存しておいた。

slow joiner問題にはきっちり直面して、このドキュメントの中の説明にも目を通しておいた。
ØMQ - The Guide - ØMQ - The Guide

サブを先に起動しておいて、

$ python3 poem_sub.py
b'five_chars' b'queen'
b'vowels' b'of'
b'five_chars' b'Lying'
b'vowels' b'at'
b'vowels' b'ease'
b'vowels' b'evening'
b'five_chars' b'flies'
b'five_chars' b'seize'
b'vowels' b'All'
b'five_chars' b'gaily'
b'five_chars' b'great'
b'vowels' b'admired'
b'vowels' b'a'
b'vowels' b'In'
...(以下略)...

sleep付きのpubを実行。

$ python3 poem_pub_sleep.py

これで復習課題は完全制覇!

(つづく)