#author("2024-08-15T07:22:58+09:00","default:TESLA202407","TESLA202407")
[[pi_arduino_uart_ex02.py]]

#code(Python){{
import socket
import threading
import subprocess
 
HOST = ''
PORT = 9998
clients = []
 
 
def remove_conection(con, address):
    """繧ッ繝ゥ繧、繧「繝ウ繝医→謗・邯壹r蛻・k"""
 
    print('[蛻・妙]{}'.format(address))
    con.close()
    clients.remove((con, address))
 
 
def server_start():
    """繧オ繝シ繝舌r繧ケ繧ソ繝シ繝医☆繧・""
 
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    sock.bind((HOST, PORT))
    sock.listen(10)
    subprocess.Popen("python3 /home/pi/python/pi_arduino_uart_ex02.py",shell=True)
    while True:
        con, address = sock.accept()
        print("[謗・邯咯{}".format(address))
        clients.append((con, address))
        handle_thread = threading.Thread(target=handler,
                                         args=(con, address),
                                         daemon=True)
        handle_thread.start()
 
 
def handler(con, address):
    """繧ッ繝ゥ繧、繧「繝ウ繝医°繧峨ョ繝シ繧ソ繧貞女菫。縺吶k"""
 
    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, data.decode("utf-8")))
                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()

}}

トップ   編集 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS