2017年1月21日土曜日

開発環境

Introducing Python(Bill Lubanovic (著)、O'Reilly Media)のChapter 11(Concurrency and Networks)、Things to Do 11.3.を解いてみる。

Things to Do 11.3.

コード

server

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

import datetime
from xmlrpc.server import SimpleXMLRPCServer


def get_current_time():
    return 'Current time: {0}'.format(datetime.datetime.utcnow().isoformat())

host = 'localhost'
port = 6789
address = (host, port)

server = SimpleXMLRPCServer(address)
server.register_function(get_current_time, 'get_current_time')
server.serve_forever()

client

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

import xmlrpc.client

host = 'localhost'
port = 6789

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

入出力結果(Terminal, IPython)

server

$ ./sample3_server.py
127.0.0.1 - - [21/Jan/2017 16:28:30] "POST / HTTP/1.1" 200 -
^CTraceback (most recent call last):
  File "./sample3_server.py", line 17, in <module>
    server.serve_forever()
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/socketserver.py", line 236, in serve_forever
    ready = selector.select(poll_interval)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/selectors.py", line 323, in select
    r, w, _ = self._select(self._readers, self._writers, [], timeout)
KeyboardInterrupt
$

client

$ ./sample3_client.py
Current time: 2017-01-21T07:28:30.603539
$

0 コメント:

コメントを投稿