-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
58 lines (39 loc) · 1.2 KB
/
Copy pathserver.py
File metadata and controls
58 lines (39 loc) · 1.2 KB
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
import socket
import threading
import clients as clients
HOST = '127.0.0.1'
PORT = 9090
server = socket .socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((HOST, PORT))
server.listen()
clinets = []
nicknames = []
def brodcast(message):
for client in clinets:
client.send(message)
def handle(client):
while True:
try:
message = client.recv(1024)
print(f"{nicknames[clinets.index(client)]}")
brodcast(message)
except:
index = clinets.index(client)
clients.close()
nicknames = nicknames[index]
nicknames.remove(nicknames)
break
def recevice():
while True:
clinet, address = server.accept()
print(f"Connected with {str(address)}!")
clinet.send("NICK".encode('utf-8'))
nickname = clinet.recv(1024)
clients.append(clinet)
print(f"Nickname of the client is {nickname}")
brodcast(f"{nickname} connected to the server!\n".encode('utf-8'))
client.send("Connected to the server".encode('utf-8'))
thread = threading.Thread(target=handle, args=(clinet,))
thread.start()
print("Server Running")
recevice()