chat_server_ex01.py
をテンプレートにして作成
[
トップ
] [
新規
|
一覧
|
検索
|
最終更新
|
ヘルプ
|
ログイン
]
開始行:
[[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_STRE...
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADD...
sock.bind((HOST, PORT))
sock.listen(10)
while True:
try:
con, address = sock.accept()
print("[接続]{}".format(address))
clients.append((con, address))
handle_thread = threading.Thread(target=handl...
args=(con, addre...
daemon=True)
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:
if not fx:
print(f'remove_connection, address={addre...
remove_conection(con, address)
return
else:
try:
l=fx.readline()
if l!="" and l!='\n' and l!=None:
#data = con.recv(1024)
print("[受信]{} - {}".format(addr...
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={a...
print(f'remove_connection, address={a...
remove_conection(con, address)
return
except ConnectionResetError:
print(f'connection reset error.address={addre...
remove_conection(con, address)
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"eth0:{ipx.ipaddr('eth0')}")
server_start()
}}
----
#counter
終了行:
[[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_STRE...
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADD...
sock.bind((HOST, PORT))
sock.listen(10)
while True:
try:
con, address = sock.accept()
print("[接続]{}".format(address))
clients.append((con, address))
handle_thread = threading.Thread(target=handl...
args=(con, addre...
daemon=True)
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:
if not fx:
print(f'remove_connection, address={addre...
remove_conection(con, address)
return
else:
try:
l=fx.readline()
if l!="" and l!='\n' and l!=None:
#data = con.recv(1024)
print("[受信]{} - {}".format(addr...
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={a...
print(f'remove_connection, address={a...
remove_conection(con, address)
return
except ConnectionResetError:
print(f'connection reset error.address={addre...
remove_conection(con, address)
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"eth0:{ipx.ipaddr('eth0')}")
server_start()
}}
----
#counter
ページ名: