Skip to content

Instantly share code, notes, and snippets.

@wiredrawing
Last active July 9, 2018 13:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wiredrawing/22a89ce0bfc8c2a459ec104da37b1f1b to your computer and use it in GitHub Desktop.
Save wiredrawing/22a89ce0bfc8c2a459ec104da37b1f1b to your computer and use it in GitHub Desktop.
Pythonでソケットを使った単一のソケットサーバー VS 複数のソケットクライアントの双方向チャット。 ref: https://qiita.com/1000VICKY/items/2338852a41c6aaf8efbb
import socket
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = "127.0.0.1"
port = 55580
s.connect((host, port))
except Exception as e:
print(e)
print(s)
while (True):
try:
# サーバ側へ送信するメッセージ
your_input = input(">>> ")
if len(your_input) > 0:
s.send(your_input.encode("UTF-8"))
# サーバーからのレスポンス
while(True):
s.settimeout(3)
res = s.recv(4096)
print(res.decode())
if (len(res) == 0):
# 受信内容を出力
break
else:
continue
except Exception as e:
print(e)
continue
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# 接続先
host = "www.yahoo.co.jp"
port = 80
sock.connect((host, port));
req = "GET / HTTP/1.1" + "\r\n\r\n";
sock.send(req.encode("UTF-8"));
read_size = 128
res = "".encode("UTF-8")
while True:
try:
sock.settimeout(3)
t = sock.recv(read_size);
res += t
if (len(t) == 0) :
print(res.decode())
print("====サーバーからの読み取り終了====")
break;
#end
except Exception as e:
print(e);
# 受信結果
...
...
...
Copyright (C) 2018 Yahoo Japan Corporation. All Rights Reserved.
</address>
</div><!--/#footer-->
</div><!--/#wrapper-->
</body>
</html>
====サーバーからの読み取り終了====
t = sock.recv(read_size);
# タイムアウトの例外をキャッチさせる
sock.settimeout(3);
res = s.recv(4096)
s.settimeout(3)
import socket
import threading
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# 接続先
host = "127.0.0.1"
port = 55580
sock.connect((host, port));
def Handler(sock):
while True:
try:
read = sock.recv(4096); #(3)
print("読み込んだバイト数:({})".format(len(read)));
print("<"+read.decode()+">");
if (len(read) < 4096) :
continue
#end
except Exception as e:
continue
while (True):
your_input = input(">>>"); #(1)
print(sock.send(your_input.encode("UTF-8"))); #(2)
thread = threading.Thread(target = Handler, args= (sock,), daemon= True)
thread.start();
import socket;
import select;
import threading
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# 接続待ちするサーバのホスト名とポート番号を指定
host = "127.0.0.1";
port = 55580
argument = (host, port)
sock.bind(argument)
# 5 ユーザまで接続を許可
sock.listen(5)
clients = []
# 接続済みクライアントは読み込みおよび書き込みを繰り返す
def loop_handler(connection, address):
while True:
try:
#クライアント側から受信する
res = connection.recv(4096)
for value in clients:
if value[1][0] == address[0] and value[1][1] == address[1] :
print("クライアント{}:{}から{}というメッセージを受信完了".format(value[1][0], value[1][1], res))
else:
value[0].send("クライアント{}:{}から{}を受信".format(value[1][0], value[1][1], res.decode()).encode("UTF-8"))
pass
except Exception as e:
print(e);
break;
while True:
try:
# 接続要求を受信
conn, addr = sock.accept()
except KeyboardInterrupt:
sock.close()
exit()
break
# アドレス確認
print("[アクセス元アドレス]=>{}".format(addr[0]))
print("[アクセス元ポート]=>{}".format(addr[1]))
print("\r\n");
# 待受中にアクセスしてきたクライアントを追加
clients.append((conn, addr))
# スレッド作成
thread = threading.Thread(target=loop_handler, args=(conn, addr), daemon=True)
# スレッドスタート
thread.start()
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = "127.0.0.1"
port = 55580
s.bind((host, port))
s.listen(10)
clients = []
try:
s.settimeout(3)
connection, address = s.accept()
clients.append((connection, address))
while(True):
try:
connection.settimeout(3)
from_client = connection.recv(4096).decode()
print("クライアントから受信したメッセージ=>{}".format(from_client))
to_client = "あなたは[{}]というメッセージを送信しましたね?".format(from_client)
connection.send(to_client.encode("UTF-8"))
except Exception as e:
print(e)
continue
except Exception as e:
print(clients)
print(e)
connection.close()
s.close()
python multi_client.py
>>>先週新しいクレヨンしんちゃんの役者さんの声を一瞬だけ聞きました
93
>>>読み込んだバイト数:(162)
<クライアント127.0.0.1:59547からもうずいぶんクレヨンしんちゃんは見ていませんが、全く違和感を感じませんでしたを受信>
break
そういえば、藤原啓治はもう復帰しましたか?
61
>>>読み込んだバイト数:(97)
<クライアント127.0.0.1:59547から野原ひろし役はいつ復帰するんです?を受信>
break
python multi_client.py
>>>もうずいぶんクレヨンしんちゃんは見ていませんが、全く違和感を感じませんでした
114
読み込んだバイト数:(141)
<クライアント127.0.0.1:59548から先週新しいクレヨンしんちゃんの役者さんの声を一瞬だけ聞きましたを受信>
break
>>>読み込んだバイト数:(109)
<クライアント127.0.0.1:59548からそういえば、藤原啓治はもう復帰しましたか?を受信>
break
野原ひろし役はいつ復帰するんです?
49
>>>
python client_py.py
<socket.socket fd=456, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=0, laddr=('127.0.0.1', 59437), raddr=('127.0.0.1', 55580)>
>>>
>>>
>>>
>>>
>>>
>>> 僕の名前はやんぼー
あなたは[僕の名前はやんぼー]というメッセージを送信しましたね?
timed out
>>> きーみと僕とでヤンマーだ!
あなたは[きーみと僕とでヤンマーだ!]というメッセージを送信しましたね?
timed out
>>> 小さなものか大きなものまでなんちゃら~
あなたは[小さなものか大きなものまでなんちゃら~]というメッセージを送信しましたね?
timed out
>>>
python unit_server.py
timed out
timed out
クライアントから受信したメッセージ=>僕の名前はやんぼー
timed out
timed out
timed out
timed out
timed out
timed out
クライアントから受信したメッセージ=>きーみと僕とでヤンマーだ!
timed out
timed out
timed out
timed out
クライアントから受信したメッセージ=>小さなものか大きなものまでなんちゃら~
python multi_server.py
[アクセス元アドレス]=>127.0.0.1
[アクセス元ポート]=>59495
[アクセス元アドレス]=>127.0.0.1
[アクセス元ポート]=>59496
クライアント127.0.0.1:59495からb'Python\xe3\x81\xa7\xe3\x83\x9e\xe3\x83\xab\xe3\x83\x81\xe3\x82\xb9\xe3\x83\xac\xe3\x83\x83\xe3\x83\x89\xe6\xa5\xbd\xe3\x81\x97\xe3\x81\x84\xe3\x81\xaa!'というメッセージを受信完了
クライアント127.0.0.1:59496からb'Python\xe3\x81\xa7\xe3\x82\xbd\xe3\x82\xb1\xe3\x83\x83\xe3\x83\x88\xe3\x82\xaf\xe3\x83\xa9\xe3\x82\xa4\xe3\x82\xa2\xe3\x83\xb3\xe3\x83\x88\xe6\xa5\xbd\xe3\x81\x97\xe3\x81\x84\xe3\x81\xaa!'というメッセージを 受信完了
クライアント127.0.0.1:59495からb'Python\xe3\x81\xa7\xe3\x82\xbd\xe3\x82\xb1\xe3\x83\x83\xe3\x83\x88\xe3\x82\xb5\xe3\x83\xbc\xe3\x83\x90\xe3\x83\xbc\xe6\xa5\xbd\xe3\x81\x97\xe3\x81\x84\xe3\x81\xaa!'というメッセージを受信完了
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment