開発環境
- macOS Sierra - Apple (OS)
- Emacs (Text Editor)
- Python 3.6 (プログラミング言語)
Introducing Python(Bill Lubanovic (著)、O'Reilly Media)のChapter 11(Concurrency and Networks)、Things to Do 11.1.を解いてみる。
Things to Do 11.1.
コード
server
#!/usr/bin/env python3
#-*- coding: utf-8 -*-
import datetime
import socket
server_address = ('localhost', 6789)
max_size = 1000
print('Starting the server')
print('Waiting for a client to call.')
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(server_address)
server.listen(10)
client, addr = server.accept()
data = client.recv(max_size)
print(client)
if data == b'time':
client.sendall('Current time: {0}'.format(
datetime.datetime.utcnow().isoformat()).encode('utf-8'))
client.close()
server.close()
client
#!/usr/bin/env python3
#-*- coding: utf-8 -*-
import datetime
import socket
server_address = ('localhost', 6789)
max_size = 1000
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(server_address)
client.sendall(b'time')
data = client.recv(max_size)
print(data.decode('utf-8'))
client.close()
入出力結果(Terminal, IPython)
server
$ ./sample1_server.py
Starting the server
Waiting for a client to call.
<socket.socket fd=6, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=0, laddr=('127.0.0.1', 6789), raddr=('127.0.0.1', 61590)>
$
client
$ ./sample1_client.py Current time: 2017-01-19T06:03:11.108045 $
0 コメント:
コメントを投稿