tcp_server_ex01.py
をテンプレートにして作成
[
トップ
] [
新規
|
一覧
|
単語検索
|
最終更新
|
ヘルプ
|
ログイン
]
開始行:
[[Real2Virtual202111]]
* tcp_server_ex01.py [#id2943ba]
#code(python){{
import socket
import threading
import subprocess
HOST = ''
PORT = 9998
clients = []
def remove_conection(con, address):
"""クライアントと接続を切る"""
print('[切断]{}'.format(address))
con.close()
clients.remove((con, address))
def server_start():
"""サーバをスタートする"""
sock = socket.socket(socket.AF_INET, socket.SOCK_STRE...
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADD...
sock.bind((HOST, PORT))
sock.listen(10)
subprocess.Popen("python3 /home/pi/python/pi_arduino_...
while True:
con, address = sock.accept()
print("[接続]{}".format(address))
clients.append((con, address))
handle_thread = threading.Thread(target=handler,
args=(con, addre...
daemon=True)
handle_thread.start()
def handler(con, address):
"""クライアントからデータを受信する"""
while True:
try:
data = con.recv(1024)
except ConnectionResetError:
remove_conection(con, address)
break
else:
if not data:
remove_conection(con, address)
break
else:
print("[受信]{} - {}".format(address, dat...
for c in clients:
if c[0]!=con:
while data:
n = c[0].sendto(data,c[1])
data = data[n:]
if __name__ == "__main__":
server_start()
}}
----
#counter
終了行:
[[Real2Virtual202111]]
* tcp_server_ex01.py [#id2943ba]
#code(python){{
import socket
import threading
import subprocess
HOST = ''
PORT = 9998
clients = []
def remove_conection(con, address):
"""クライアントと接続を切る"""
print('[切断]{}'.format(address))
con.close()
clients.remove((con, address))
def server_start():
"""サーバをスタートする"""
sock = socket.socket(socket.AF_INET, socket.SOCK_STRE...
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADD...
sock.bind((HOST, PORT))
sock.listen(10)
subprocess.Popen("python3 /home/pi/python/pi_arduino_...
while True:
con, address = sock.accept()
print("[接続]{}".format(address))
clients.append((con, address))
handle_thread = threading.Thread(target=handler,
args=(con, addre...
daemon=True)
handle_thread.start()
def handler(con, address):
"""クライアントからデータを受信する"""
while True:
try:
data = con.recv(1024)
except ConnectionResetError:
remove_conection(con, address)
break
else:
if not data:
remove_conection(con, address)
break
else:
print("[受信]{} - {}".format(address, dat...
for c in clients:
if c[0]!=con:
while data:
n = c[0].sendto(data,c[1])
data = data[n:]
if __name__ == "__main__":
server_start()
}}
----
#counter
ページ名: