-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
62 lines (53 loc) · 1.98 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
55
56
57
58
59
60
61
62
import socket
import _thread
class Server:
def __init__(self):
self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.port = 6666
self.address = 'ADDRESS'
self.connections = {}
def setup(self):
self.server.bind((self.address, self.port))
self.server.listen() # Listen for client connections
def client_thread(self, conn):
data = conn.recv(4096)
decoded = data.decode()
print(decoded)
name, discrim = decoded.split(',')
conn.send(f'Successfully connected.'.encode())
conn_id = len(self.connections)
conn.send(f'There are {len(conn_id) - 1} users online.'.encode())
self.forward(f'{name}#{discrim} has joined the chat room. Say hi!', conn_id)
self.connections[conn_id] = conn
while True:
try:
data = conn.recv(4096)
decoded = data.decode()
if not decoded:
break
print(f'Data received: {decoded}')
self.forward(decoded, conn_id)
except Exception as _:
print(_)
break
self.connections.pop(conn_id)
self.forward(f'{name}#{discrim} has left the chat room.', conn_id)
def send_to(self, send_id: int, text: str):
conn = self.connections[send_id]
conn.send(text.encode())
def forward(self, text: str, avoid_id: int):
encoded = text.encode()
for conn_id, conn in self.connections.items():
if conn_id != avoid_id:
try:
conn.send(encoded)
except Exception as _:
raise _
print('Starting server...')
s = Server()
s.setup()
print('Setup complete. Now waiting for a connection.')
while True:
conn, address = s.server.accept()
print(f'Connection from {address}')
_thread.start_new_thread(s.client_thread, (conn,))