tcp_server_ex1.py
をテンプレートにして作成
[
トップ
] [
新規
|
一覧
|
単語検索
|
最終更新
|
ヘルプ
|
ログイン
]
開始行:
[[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_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):
"""繧ッ繝ゥ繧、繧「繝ウ繝医°繧峨ョ繝シ繧ソ繧貞女菫。縺吶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, d...
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()
}}
終了行:
[[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_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):
"""繧ッ繝ゥ繧、繧「繝ウ繝医°繧峨ョ繝シ繧ソ繧貞女菫。縺吶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, d...
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()
}}
ページ名: