-
Notifications
You must be signed in to change notification settings - Fork 0
/
Server.py
54 lines (42 loc) · 1.29 KB
/
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import socket
import threading
HOST = socket.gethostbyname(socket.gethostname()) #ipconfig
PORT = 9060
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((HOST, PORT))
server.listen()
clients = []
names = []
print(f'loading server...')
def broadcast(msg):
for client in clients:
client.send(msg)
def handle(client):
while True:
try:
msg = client.recv(1024)
print(f'{names[clients.index(client)]}: {msg}')
broadcast(msg)
except:
index = clients.index(client)
clients.remove(client)
client.close()
nickname = names[index]
print(f'{nickname} disconnected!')
names.remove(nickname)
break
def receive():
while True:
client, address = server.accept()
print(f'{str(address)} connected :)')
client.send('NAME: '.encode('utf-8'))
name = client.recv(1024)
names.append(name)
clients.append(client)
print(f'Names of clients {names}')
broadcast(f'{name} connected!\n'.encode('utf-8'))
client.send(f'Connected to the SERVER'.encode('utf-8'))
thread = threading.Thread(target=handle, args=(client, ))
thread.start()
print(f'server running >>>')
receive()