開発環境
- OS X El Capitan - Apple (OS)
- Emacs (Text Editor)
- Python 3.5 (プログラミング言語)
入門 Python 3 (Bill Lubanovic (著)、 斎藤 康毅(監修)、 長尾 高弘 (翻訳)、オライリージャパン)の11章(並行処理とネットワーク)、11.3(復習問題)、11-2を取り組んでみる。
11-2
コード(Emacs)
server.py
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import zmq
import datetime
host = '127.0.0.1'
port = 6789
print('Starting the server at {0}(ZeroMQ)\n'
'Waiting for a client to call.'.format(datetime.datetime.utcnow()))
context = zmq.Context()
server = context.socket(zmq.REP)
server.bind('tcp://{0}:{1}'.format(host, port))
while True:
request = server.recv_unicode()
if request == 'time':
server.send_unicode(datetime.datetime.utcnow().isoformat())
break
else:
server.send_unicode('?????')
server.close()
context.destroy()
client.py
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import zmq
import datetime
host = '127.0.0.1'
port = 6789
print('Starting the client at {0}(ZeroMQ)'.format(datetime.datetime.utcnow()))
context = zmq.Context()
client = context.socket(zmq.REQ)
client.connect('tcp://{0}:{1}'.format(host, port))
for word in ['Hello', 'time']:
client.send_unicode(word)
reply = client.recv_unicode()
print(reply)
client.close()
context.destroy()
入出力結果(Terminal, IPython)
$ ./server2.py > output.txt & [1] 9396 $ ./client2.py Starting the client at 2016-09-05 06:14:57.663949(ZeroMQ) ????? 2016-09-05T06:14:57.671902 [1]+ Done ./server2.py > output.txt $ cat output.txt Starting the server at 2016-09-05 06:14:52.881229(ZeroMQ) Waiting for a client to call. $
0 コメント:
コメントを投稿