-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
75 lines (60 loc) · 2.02 KB
/
Copy pathserver.py
File metadata and controls
75 lines (60 loc) · 2.02 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# server.py
import socket
from _thread import *
import sys
class Server:
def __init__(self, ip, port):
self.ip = ip
self.port = port
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.users = []
self.clients = []
self.running = True
def connect(self):
try:
self.socket.bind((self.ip, self.port))
except socket.error as e:
return False, str(e)
return True, 'Connected'
def listen(self):
self.socket.listen(2)
print('[SERVER] listening...')
while self.running:
conn, addr = self.socket.accept()
self.clients.append(conn)
print("[SERVER] Connected to:", addr)
start_new_thread(self.threaded_client, (conn,))
def broadcastUserList(self):
self.sendToAll(str.encode('USERS|'+str(self.users)))
def sendToAll(self, msg):
print("[SERVER-BROADCAST]>> ", msg)
for conn in self.clients:
conn.sendall(msg)
def stop(self):
#self.socket.shutdown()
self.running = False
self.socket.close()
def threaded_client(self, conn):
conn.sendall(str.encode("CONNECTED"))
reply = ""
while True:
try:
data = conn.recv(2048)
reply = data.decode("utf-8")
if not data:
print("Disconnected")
break
else:
print("[SERVER]<< ", reply)
cmd, username = reply.split('|')
if cmd=='NEW':
self.users.append(username)
self.broadcastUserList()
except Exception as e:
print(e)
break
print('[SERVER]', username, 'teminated!')
self.clients.remove(conn)
self.users.remove(username)
self.broadcastUserList()
conn.close()