#author("2026-05-10T11:31:54+09:00","default:TESLA2","TESLA2")
#author("2026-05-26T22:37:56+09:00","default:TESLA2","TESLA2")
[[TESLA2]]

#code(python){{
import socket
import threading
import subprocess
import ipget

HOST = ''
PORT = 9998
clients = []


def remove_conection(con, address):
    """クライアントと接続を切る"""

    global clients
    print('[切断]{}'.format(address))
    con.close()
    clients.remove((con, address))


def server_start():
    """サーバをスタートする"""

    global clients
    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/tesla_transformer_20240813_01.py",shell=True)
    #subprocess.Popen("python3 /home/pi/python/tesla_transformer_prove_20251226_01.py",shell=True)
    while True:
        con, address = sock.accept()
        print("[接続]{}".format(address))
        clients.append((con, address))
        handle_thread = threading.Thread(target=handler,
        try:
            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()
            handle_thread.start()
        except Exception as e:
            print(f'accept error...{str(e)}')


def handler(con, address):
    """クライアントからデータを受信する"""

    global clients
    try:
        fx=con.makefile('r')
    except ConnectionResetError:
        remove_conection(con, address)
        return
    except Exception as e:
        print(e)
        return
    while True:
        try:
            data = con.recv(1024)
            if not fx:
                print(f'remove_connection, address={address}')
                remove_conection(con, address)
                return
            else:
                try:
                    l=fx.readline()
                    if l!="" and l!='\n' and l!=None:
                        #data = con.recv(1024)
                        print("[受信]{} - {}".format(address, l))
                        for c in clients:
                            if c[0]!=con:
                                c[0].send(bytes(l,"utf-8"))
                except Exception as e:
                    print(f'Exception:{str(e)}.address={address}')
                    print(f'remove_connection, address={address}')
                    remove_conection(con, address)
                    return
        except ConnectionResetError:
            print(f'connection reset error.address={address}')
            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:]
            return
        except Exception as e:
            print(f'Exception:{str(e)}.address={address}')
            return

if __name__ == "__main__":
    ipx=ipget.ipget()
    print(f"wlan0:{ipx.ipaddr('wlan0')}")
    #print(f"wlan0:{ipx.ipaddr('wlan0')}")
    print(f"eth0:{ipx.ipaddr('eth0')}")
    server_start()

}}
----
#counter

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