開発環境
- OS X Mavericks - Apple(OS)
- Emacs (CUI)、BBEdit - Bare Bones Software, Inc. (GUI) (Text Editor)
- Python 3.4 (プログラミング言語)
Head First C ―頭とからだで覚えるCの基本(David Griffiths (著)、Dawn Griffiths (著) 中田 秀基(監訳)(翻訳)、木下 哲也 (翻訳)、オライリージャパン)の11章(ソケットとネットワーキング(127.0.0.1という場所はない)、自分で考えてみよう(p.487)をpythonで考えてみる。
自分で考えてみよう(p.487)
コード(BBEdit, Emacs)
sample487.py
#!/usr/bin/env python3
#-*- coding: utf-8 -*-
import sys
import socket
import signal
def error(msg):
print("{0}: {1}".format(msg, err))
sys.exit(1)
def say(s):
try:
data = s.encode('utf-8')
count = connect_d.send(data)
if count != len(data):
raise Exception()
except Exception as err:
raise Exception('クライアントの通信エラー: {0}'.format(err))
def handle_shutdown(sig, stack_frame):
if listener_d != None:
listener_d.close()
print('さようなら!', file=sys.stderr)
sys.exit(0)
try:
signal.signal(signal.SIGINT, handle_shutdown)
except Exception as err:
error('割り込みハンドラを設定できません。{0}'.format(err))
port = 30000
host = 'localhost'
listener_d = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
listener_d.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
except Exception as err:
error('ソケットに再利用オプションを設定できません。{0}'.format(err))
listener_d.bind((host, port))
try:
listener_d.listen(10)
except Exception as err:
error('接続待ちできません。{0}'.format(err))
print("接続を待っています。")
while True:
try:
connect_d, addr = listener_d.accept()
except Exception as err:
error('第2のソケットを開けません。{0}'.format(err))
try:
say('Knock!Knock!\r\n')
data = connect_d.recv(255)
if data[:12] == b"Who's there?":
try:
say('Oscar\r\n')
data = connect_d.recv(255)
try:
if data[:10] == b'Oscar who?':
say('Oscar silly question, you get a silly answer\r\n')
else:
say('「Oscar who?」と入力しなければいけません!\r\n')
except Exception as err:
print(err, file=sys.stderr)
except Exception as err:
print(err, file=sys.stderr)
else:
say("「Who's there?」と入力しなければいけません!\r\n")
except Exception as err:
print(err, file=sys.stderr)
connect_d.close()
入出力結果(Terminal, IPython)
サーバー
$ ./sample487.py 接続を待っています。 C-c C-cさようなら! $
同時に複数のやり取り。
クライアント1
$ telnet 127.0.0.1 30000 Trying 127.0.0.1... Connected to localhost. Escape character is '^]'. Knock!Knock! Who's there? Who's there? Oscar Oscar who? Oscar who? Oscar silly question, you get a silly answer Connection closed by foreign host. $
クライアント2
$ telnet 127.0.0.1 30000 Trying 127.0.0.1... Connected to localhost. Escape character is '^]'. Knock!Knock! Who's there? Who's there? Oscar Oscar who? Oscar who? Oscar silly question, you get a silly answer Connection closed by foreign host. $
0 コメント:
コメントを投稿