Client upgrade with cmd or not functions, Server commands system, GUI base
This commit is contained in:
parent
734aa6f3fd
commit
957ac53c2c
3 changed files with 99 additions and 17 deletions
32
client.py
32
client.py
|
@ -10,20 +10,18 @@ class Client(Socket):
|
||||||
self.service_id = service_id
|
self.service_id = service_id
|
||||||
print("Connecting...")
|
print("Connecting...")
|
||||||
self.connect_server(host, port)
|
self.connect_server(host, port)
|
||||||
Thread(target=self.receive_server).start()
|
|
||||||
Thread(target=self.send_input).start()
|
|
||||||
|
|
||||||
def receive_server(self):
|
def receive_server(self):
|
||||||
while True:
|
try:
|
||||||
try:
|
data = self.receive(self.socket)
|
||||||
data = self.receive(self.socket)
|
except socket_error:
|
||||||
except socket_error:
|
self.quit()
|
||||||
|
return False
|
||||||
|
else:
|
||||||
|
if data.lower() == "quit":
|
||||||
self.quit()
|
self.quit()
|
||||||
else:
|
return False
|
||||||
if data.lower() == "quit":
|
return data
|
||||||
self.quit()
|
|
||||||
break
|
|
||||||
print(data)
|
|
||||||
|
|
||||||
def send_server(self, data):
|
def send_server(self, data):
|
||||||
try:
|
try:
|
||||||
|
@ -38,6 +36,14 @@ class Client(Socket):
|
||||||
if data.lower() == "quit":
|
if data.lower() == "quit":
|
||||||
break
|
break
|
||||||
|
|
||||||
|
def receive_print(self):
|
||||||
|
while True:
|
||||||
|
message = self.receive_server()
|
||||||
|
if message:
|
||||||
|
print(message)
|
||||||
|
else:
|
||||||
|
break
|
||||||
|
|
||||||
def quit(self):
|
def quit(self):
|
||||||
try:
|
try:
|
||||||
self.socket.close()
|
self.socket.close()
|
||||||
|
@ -49,4 +55,6 @@ class Client(Socket):
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
Client("localhost", 3621)
|
client = Client("localhost", 3621)
|
||||||
|
Thread(target=client.receive_print).start()
|
||||||
|
Thread(target=client.send_input).start()
|
||||||
|
|
72
gui.py
Normal file
72
gui.py
Normal file
|
@ -0,0 +1,72 @@
|
||||||
|
from tkinter import Tk, Frame, Scrollbar, Label, Text, Button, Entry, StringVar, IntVar, LEFT, RIGHT, BOTH, Y, END
|
||||||
|
from tkinter.messagebox import showerror
|
||||||
|
from client import Client
|
||||||
|
from threading import Thread
|
||||||
|
|
||||||
|
|
||||||
|
def on_closing():
|
||||||
|
try:
|
||||||
|
client.quit()
|
||||||
|
except NameError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
def start():
|
||||||
|
if host.get() and port.get():
|
||||||
|
try:
|
||||||
|
global client
|
||||||
|
client = Client(host.get(), port.get())
|
||||||
|
except ConnectionError:
|
||||||
|
showerror("Error", "can't connect tu server !")
|
||||||
|
else:
|
||||||
|
login.destroy()
|
||||||
|
|
||||||
|
|
||||||
|
def receive():
|
||||||
|
while True:
|
||||||
|
msg = client.receive_server()
|
||||||
|
if msg.lower() == "quit":
|
||||||
|
break
|
||||||
|
if msg[-1:] != "\n":
|
||||||
|
msg += "\n"
|
||||||
|
chat_message.insert(END, msg)
|
||||||
|
|
||||||
|
|
||||||
|
def send(event=None):
|
||||||
|
client.send_server(message.get())
|
||||||
|
if message.get().lower() == "quit":
|
||||||
|
tchat.destroy()
|
||||||
|
message.set("")
|
||||||
|
|
||||||
|
|
||||||
|
login = Tk()
|
||||||
|
login.title("Login")
|
||||||
|
host = StringVar()
|
||||||
|
port = IntVar()
|
||||||
|
Label(login, text="Host & port:").pack()
|
||||||
|
login_f = Frame(login)
|
||||||
|
login_f.pack()
|
||||||
|
Entry(login_f, textvariable=host, width=14).grid(row=0, column=0)
|
||||||
|
Entry(login_f, textvariable=port, width=4).grid(row=0, column=1)
|
||||||
|
Button(login, text="Submit", command=start).pack()
|
||||||
|
login.mainloop()
|
||||||
|
|
||||||
|
tchat = Tk()
|
||||||
|
tchat.title("PyTchat")
|
||||||
|
tchat.protocol("WM_DELETE_WINDOW", on_closing)
|
||||||
|
chat = Frame(tchat)
|
||||||
|
chat.pack()
|
||||||
|
scrollbar = Scrollbar(chat)
|
||||||
|
scrollbar.pack(side=RIGHT, fill=Y)
|
||||||
|
chat_message = Text(chat, height=15, width=50, yscrollcommand=scrollbar.set)
|
||||||
|
chat_message.pack(side=LEFT, fill=BOTH)
|
||||||
|
Thread(target=receive).start()
|
||||||
|
|
||||||
|
entry = Frame(tchat)
|
||||||
|
entry.pack()
|
||||||
|
message = StringVar()
|
||||||
|
field = Entry(entry, textvariable=message)
|
||||||
|
field.bind("<Return>", send)
|
||||||
|
field.grid(row=0, column=0)
|
||||||
|
Button(entry, text="Send", command=send).grid(row=0, column=1)
|
||||||
|
tchat.mainloop()
|
12
server.py
12
server.py
|
@ -5,25 +5,29 @@ from socket import error as socket_error
|
||||||
|
|
||||||
class Server(Socket):
|
class Server(Socket):
|
||||||
def __init__(self, host: str = "localhost", port: int = 3621, service_id: int = 2):
|
def __init__(self, host: str = "localhost", port: int = 3621, service_id: int = 2):
|
||||||
|
print("Server start")
|
||||||
super().__init__()
|
super().__init__()
|
||||||
self.socket.bind((host, port))
|
self.socket.bind((host, port))
|
||||||
self.socket.listen(5)
|
self.socket.listen(5)
|
||||||
self.service_id = service_id
|
self.service_id = service_id
|
||||||
self.command_suffix = "!"
|
self.command_suffix = "!"
|
||||||
|
self.welcom_msg = "Welcom ! Type \"!help\" to see commands and \"quit\" to exit"
|
||||||
self.commands = {"help": self.command_help, "players list": self.command_players_list}
|
self.commands = {"help": self.command_help, "players list": self.command_players_list}
|
||||||
self.clients = dict()
|
self.clients = dict()
|
||||||
|
print("Wait for connexion...")
|
||||||
Thread(target=self.connexion).start()
|
Thread(target=self.connexion).start()
|
||||||
|
|
||||||
def connexion(self):
|
def connexion(self):
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
c, address = self.connect_client(self.socket)
|
c, address = self.connect_client(self.socket)
|
||||||
except socket_error:
|
except (socket_error, ConnectionError):
|
||||||
continue
|
continue
|
||||||
else:
|
else:
|
||||||
name = self.client_name(c)
|
name = self.client_name(c)
|
||||||
if name:
|
if name:
|
||||||
self.clients[name] = c
|
self.clients[name] = c
|
||||||
|
self.send(self.clients[name], self.welcom_msg)
|
||||||
self.broadcast(f"{name} is online !")
|
self.broadcast(f"{name} is online !")
|
||||||
Thread(target=self.listen_client, args=(name,)).start()
|
Thread(target=self.listen_client, args=(name,)).start()
|
||||||
|
|
||||||
|
@ -54,13 +58,11 @@ class Server(Socket):
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
if not self.command(data, name):
|
if not self.command(data, name):
|
||||||
Thread(target=self.broadcast, args=(f"{name}: {data}", name)).start()
|
Thread(target=self.broadcast, args=(f"{name}: {data}",)).start()
|
||||||
|
|
||||||
def broadcast(self, message, author=None):
|
def broadcast(self, message):
|
||||||
print(message)
|
print(message)
|
||||||
for i in self.clients:
|
for i in self.clients:
|
||||||
if i == author:
|
|
||||||
continue
|
|
||||||
try:
|
try:
|
||||||
self.send(self.clients[i], message)
|
self.send(self.clients[i], message)
|
||||||
except socket_error:
|
except socket_error:
|
||||||
|
|
Reference in a new issue