開発環境
- OS X Yosemite - Apple (OS)
- Emacs (CUI)、BBEdit - Bare Bones Software, Inc. (GUI) (Text Editor)
- Python 3.4 (プログラミング言語)
Introducing Python: Modern Computing in Simple Packages(Bill Lubanovic (著)、 O'Reilly Media)のChapter 11(Concurrency and Networks)、Things to Do 11.3.を解いてみる。
Things to Do 11.3.
コード(Eacs, BBEdit)
server
#!/usr/bin/env python3
#-*- coding: utf-8 -*-
import datetime
import xmlrpc.server
def func(msg):
print('recieved', msg)
if msg == 'time':
now = datetime.datetime.now()
return datetime.datetime.isoformat(now)
else:
return '...'
server_address = ('localhost', 6789)
server = xmlrpc.server.SimpleXMLRPCServer(server_address)
server.register_function(func, 'func')
server.serve_forever()
client
#!/usr/bin/env python3
#-*- coding: utf-8 -*-
import xmlrpc.client
server_address = ('localhost', 6789)
proxy = xmlrpc.client.ServerProxy('http://{0}:{1}/'.format(*server_address))
msg = ''
while True:
msg = input()
if msg == 'q':
break
print('send', msg)
print(proxy.func(msg))
入出力結果(Terminal, IPython)
server
$ ./sample3_server.py recieved time 127.0.0.1 - - [09/May/2015 16:29:34] "POST / HTTP/1.1" 200 - recieved Hello 127.0.0.1 - - [09/May/2015 16:29:40] "POST / HTTP/1.1" 200 - recieved time 127.0.0.1 - - [09/May/2015 16:29:41] "POST / HTTP/1.1" 200 - C-c C-cTraceback (most recent call last): File "./sample3_server.py", line 18, in <module> server.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 $
client
$ ./sample3_client.py time send time 2015-05-09T16:29:34.755010 Hello send Hello ... time send time 2015-05-09T16:29:41.943005 q $
0 コメント:
コメントを投稿