-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
32 lines (29 loc) · 879 Bytes
/
server.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
import socket
import threading
def client_thread(conn, clients):
while True:
try:
data = conn.recv(1024)
if not data:
break
for c in clients:
if c != conn:
c.sendall(data)
except:
break
conn.close()
clients.remove(conn)
print("Client disconnected")
def start_server(host, port):
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind((host, port))
server_socket.listen()
clients = []
print("Serverin dinlədiyi ünvan", host, port)
while True:
conn, addr = server_socket.accept()
clients.append(conn)
print(f"{addr} qoşulub")
threading.Thread(target=client_thread, args=(conn, clients)).start()
if __name__ == "__main__":
start_server('127.0.0.1', 999)