2016年9月6日火曜日

開発環境

入門 Python 3 (Bill Lubanovic (著)、 斎藤 康毅(監修)、 長尾 高弘 (翻訳)、オライリージャパン)の11章(並行処理とネットワーク)、11.3(復習問題)、11-3を取り組んでみる。

11-3

コード(Emacs)

server.py

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

from xmlrpc.server import SimpleXMLRPCServer
import datetime

def now(word):
    if word == 'time':
        return datetime.datetime.utcnow().isoformat()
    else:
        return '?????'
    
host = '127.0.0.1'
port = 6789

server = SimpleXMLRPCServer((host, port))
server.register_function(now, 'now')
server.serve_forever()

client.py

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

import xmlrpc.client
import datetime

host = '127.0.0.1'
port = 6789

proxy = xmlrpc.client.ServerProxy('http://{0}:{1}'.format(host, port))

for word in ['Hello', 'time']:
    result = proxy.now(word)
    print(result)

入出力結果(Terminal, IPython)

$ ./server3.py &
[1] 12121
$ ./client3.py
127.0.0.1 - - [06/Sep/2016 12:17:32] "POST /RPC2 HTTP/1.1" 200 -
?????
127.0.0.1 - - [06/Sep/2016 12:17:32] "POST /RPC2 HTTP/1.1" 200 -
2016-09-06T03:17:32.837104
$ fg
./server3.py
  C-c C-cTraceback (most recent call last):
  File "./server3.py", line 18, in <module>
    server.serve_forever()
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/socketserver.py", line 232, in serve_forever
    ready = selector.select(poll_interval)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/selectors.py", line 323, in select
    r, w, _ = self._select(self._readers, self._writers, [], timeout)
KeyboardInterrupt
$ 

0 コメント:

コメントを投稿