pi_arduino_uart_ex02.py

٤Ƴ٤Ĥ
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
-
|
|
|
|
|
!
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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