client.pyとserver.pyファイルを作って、server.pyをrunningしたんですが、うまく行かないです。
以下が見本のコマンドです
python
1python3 server.py 2my ip address is now ... 10.1.1.27 ← ※1 Waiting for the connection ... 3Connected by .. ('10.1.1.27', 55017) 4Waiting for the connection ... 5Hello! This is test message from my sample client!
以下が自分のパソコンで、コマンドの操作が途中で止まってしまいます。何がまずいのか知りたいです。
Python
1python server.py 2my ip address is now ... 192.168.100.101 3Waiting for the connection ... 4
以下が作ったファイルです
python
1#client.py 2#coding:utf-8 3import socket 4 5my_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 6#ここは環境に合わせた接続先を入れる 7my_socket.connect(('10.1.1.27', 50030)) 8my_text = "Hello! This is test message from my sample client!" 9my_socket.sendall(my_text.encode('utf-8')) 10
python
1#server.py 2#coding:utf-8 3from concurrent.futures import ThreadPoolExecutor 4import socket 5import os 6 7def __handle_message(args_tuple): 8 9 conn, addr, data_sum = args_tuple 10 while True: 11 data = conn.recv(1024) 12 data_sum = data_sum + data.decode('utf-8') 13 14 if not data: 15 break 16 17 if data_sum != '': 18 print(data_sum) 19 20def __get_myip(): 21 s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) 22 s.connect(('8.8.8.8', 80)) 23 return s.getsockname()[0] 24 25def main(): 26 27 #AF_INET : IPv4ベースのアドレス体系を使うということ 28 29 #SOCK_STREAM : TCP/IPを使うということ 30 my_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 31 32 #多重接続になってもいいようにスレッドで処理するようにする 33 executor = ThreadPoolExecutor(max_workers=os.cpu_count()) 34 35 #開くポート番号は適当に選んだだけ 36 myhost = __get_myip() 37 print('my ip address is now ...', myhost) 38 my_socket.bind((myhost, 50030)) 39 #同時に接続してくる相手の数。今回はテストなのでとりあえず1 40 my_socket.listen(1) 41 42 while True: 43 #接続があるまで待機 44 print('Waiting for the connection ...') 45 conn, addr = my_socket.accept() 46 print('Connected by ..', addr) 47 data_sum = '' 48 executor.submit(__handle_message, (conn, addr, data_sum)) 49 50if __name__ == '__main__': 51 main() 52
「うまくいかない」というのは、具体的にどんな状況になっていますか?エラーメッセージなどが出ているなら、実際のものを書き込んでください。