Initial commit
This commit is contained in:
commit
58e273f88f
2 changed files with 103 additions and 0 deletions
37
client.py
Normal file
37
client.py
Normal file
|
@ -0,0 +1,37 @@
|
||||||
|
from threading import Thread
|
||||||
|
from SecureSocketService import Socket
|
||||||
|
from sys import exit
|
||||||
|
|
||||||
|
|
||||||
|
class Client(Socket):
|
||||||
|
def __init__(self, host: str, port: int, service_id: int = 2):
|
||||||
|
super().__init__()
|
||||||
|
self.service_id = service_id
|
||||||
|
print("Connecting...")
|
||||||
|
self.connect_server(host, port)
|
||||||
|
self.receive_th = Thread(target=self.receive_server).start()
|
||||||
|
self.send_th = Thread(target=self.send_server).start()
|
||||||
|
|
||||||
|
def receive_server(self):
|
||||||
|
while True:
|
||||||
|
data = self.receive(self.socket)
|
||||||
|
if data.lower() == "quit":
|
||||||
|
self.quit()
|
||||||
|
break
|
||||||
|
print(data)
|
||||||
|
|
||||||
|
def send_server(self):
|
||||||
|
while True:
|
||||||
|
data = input()
|
||||||
|
self.send(self.socket, data)
|
||||||
|
if data.lower() == "quit":
|
||||||
|
break
|
||||||
|
|
||||||
|
def quit(self):
|
||||||
|
self.socket.close()
|
||||||
|
print("Exit")
|
||||||
|
exit()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
Client("localhost", 3621)
|
66
server.py
Normal file
66
server.py
Normal file
|
@ -0,0 +1,66 @@
|
||||||
|
from threading import Thread
|
||||||
|
from SecureSocketService import Socket
|
||||||
|
|
||||||
|
|
||||||
|
class Server(Socket):
|
||||||
|
def __init__(self, host: str = "localhost", port: int = 3621, service_id: int = 2):
|
||||||
|
super().__init__()
|
||||||
|
self.socket.bind((host, port))
|
||||||
|
self.socket.listen(5)
|
||||||
|
self.service_id = service_id
|
||||||
|
self.clients = dict()
|
||||||
|
Thread(target=self.connexion).start()
|
||||||
|
|
||||||
|
def connexion(self):
|
||||||
|
while True:
|
||||||
|
c, adress = self.connect_client(self.socket)
|
||||||
|
name = self.client_name(c)
|
||||||
|
if name:
|
||||||
|
self.clients[name] = c
|
||||||
|
self.broadcast(f"{name} is online !")
|
||||||
|
Thread(target=self.listen_client, args=(name,)).start()
|
||||||
|
|
||||||
|
def client_name(self, sock):
|
||||||
|
while True:
|
||||||
|
try:
|
||||||
|
self.send(sock, "Your name ?")
|
||||||
|
name = self.receive(sock)
|
||||||
|
if name in self.clients:
|
||||||
|
self.send(sock, "Name already taken !")
|
||||||
|
elif name.lower() == "quit":
|
||||||
|
sock.close()
|
||||||
|
name = None
|
||||||
|
else:
|
||||||
|
break
|
||||||
|
except:
|
||||||
|
name = None
|
||||||
|
break
|
||||||
|
return name
|
||||||
|
|
||||||
|
def listen_client(self, name):
|
||||||
|
while True:
|
||||||
|
try:
|
||||||
|
data = self.receive(self.clients[name])
|
||||||
|
assert data.lower() != "quit"
|
||||||
|
except:
|
||||||
|
self.send(self.clients[name], "quit")
|
||||||
|
self.clients[name].close()
|
||||||
|
del self.clients[name]
|
||||||
|
self.broadcast(f"{name} is offline !")
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
Thread(target=self.broadcast, args=(f"{name}: {data}", name)).start()
|
||||||
|
|
||||||
|
def broadcast(self, message, author = None):
|
||||||
|
print(message)
|
||||||
|
for i in self.clients:
|
||||||
|
if i == author:
|
||||||
|
continue
|
||||||
|
try:
|
||||||
|
self.send(self.clients[i], message)
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
Server()
|
Reference in a new issue