import socket, threading from PyQt5 import QtWidgets as qtw import os import sys import json import time from UI_Server import Ui_Server BUFSIZE = 2048 clients = [] SERVER = "" FORMAT = 'utf-8' class MainServer(qtw.QMainWindow): def __init__(self) : self.isOpen = False qtw.QMainWindow.__init__(self) self.ui = Ui_Server() self.ui.setupUi(self) self.show() self.total_clients = 0 #Event self.ui.txtPort.textChanged.connect(self.inputPort) self.ui.txtNumber.textChanged.connect(self.inputNumber) self.ui.buttonOpen.clicked.connect(self.openServer) self.ui.buttonClose.clicked.connect(self.closeServer) #def disableButton(self): #self.ui.pushButton.setEnabled(False) #----------------------------------------------------------------------- def closeServer(self): #try: self.ui.textBrowser.append("Close all connection") self.server.close() #event.accept() #except OSError: #event.accept() def inputPort(self): str(self.ui.txtPort.text()) def inputNumber(self): str(self.ui.txtNumber.text()) def openServer(self): self.start() def closeEvent(self,event): self.destroy() os._exit(0) #----------------------------------------------------------------------- def newConnection(self, server): while True: print(len(clients)) if int(self.ui.txtNumber.text()) == len(clients): self.ui.textBrowser.append("Reached maximum connection") break else: connection, client_addr = self.server.accept() clients.append(self.Client(self, connection, client_addr, True)) # add new client and waiting for it's repsonse clients[len(clients) - 1].start() self.ui.textBrowser.append(str(client_addr) + " has connected to server") #self.handle_client(connection, client_addr) #try: # except socket.error as e: # self.ui.textBrowser.append(str(client_addr) + " has disconnected") # break def start(self): self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) # reuse socket after being closed immediately self.server.bind((SERVER, int(self.ui.txtPort.text()))) self.ui.textBrowser.append("Waiting for connection") self.server.listen(5) serverThread = threading.Thread(target = self.newConnection, args = (self.server, )) serverThread.start() #-------------------------------------------------------------------------------- class Client( threading.Thread): def __init__(self, server, socket, address, signal): threading.Thread.__init__(self) self.socket = socket self.address = address self.signal = signal self.server = server def __str__(self): return str(self.address) def run(self): while self.signal: try: cmd = self.socket.recv(1024) cmd = cmd.decode() except: self.server.ui.textBrowser.append("Client " + str(self.address) + " has disconnected") self.signal = False clients.remove(self) break #---------------------------------------------------------------------------------------------------------- if cmd == 'signin': print("signin") caseS = signIn(self.socket) if caseS == 0: #signUp UNSUCESSFUL msg = "SignIn false" self.socket.send(bytes(msg, "utf-8")) elif caseS == 2: msg = "Admin SignIn successful" self.socket.send(bytes(msg, "utf-8")) self.server.ui.textBrowser.append("admin has Sing In") else: msg = "SignIn successful" self.socket.send(bytes(msg, "utf-8")) self.server.ui.textBrowser.append(str(self.address) + " has Sing In") elif cmd == "signUp": print("signup") if signUp(self.socket) == 0: msg = "SignUp false" self.socket.send(bytes(msg, "utf-8")) else: msg = "SignUp Sucessful" self.socket.send(bytes(msg, "utf-8")) self.server.ui.textBrowser.append(str(self.address) + " has Sing Up") elif cmd == "listall": sendListAll(self.socket) #--------------------------------------------------------------------------- def sendListAll(connection): THIS_FOLDER = os.path.dirname(os.path.abspath(__file__)) #deriving the current path of file my_file = os.path.join(THIS_FOLDER, 'matches.json') f = open(my_file, 'r') data = json.load(f) f.close() data = json.dumps(data) size = sys.getsizeof(data) size = str(size) connection.send(bytes(size, "utf-8")) #send size time.sleep(0.02) connection.sendall(bytes(data, "utf-8")) ##################################################################### def checkSignIn(username, password): THIS_FOLDER = os.path.dirname(os.path.abspath(__file__)) #deriving the current path of file my_file = os.path.join(THIS_FOLDER, 'data.txt') f = open(my_file,'r') #read number of clients num_clients = f.readline() num_clients = int(num_clients) for i in range(1, num_clients + 1): signin_user = f.readline() signin_user = signin_user.strip() signin_pass = f.readline() signin_pass = signin_pass.strip() signin_id = f.readline() signin_id = int(signin_id.strip()) if (signin_user == username and signin_pass == password): if signin_id == 1: #1-ADMIN #0-Client return 2 #Return TRUE else: return 1 return 0 #Return FALSE def checkUsername(username): THIS_FOLDER = os.path.dirname(os.path.abspath(__file__)) #deriving the current path of file my_file = os.path.join(THIS_FOLDER, 'data.txt') f = open(my_file,'r') num_clients = f.readline() num_clients = int(num_clients) for i in range(1, num_clients + 1): signin_user = f.readline() signin_user = signin_user.strip() signin_pass = f.readline() signin_pass = signin_pass.strip() if signin_user == username: return 0 return 1 def signIn(connection): print("join sign in") data = connection.recv(BUFSIZE) username = data.decode("utf-8") data = connection.recv(BUFSIZE) password = data.decode("utf-8") case_signin = checkSignIn(username, password) return case_signin def signUp(connection): data = connection.recv(BUFSIZE) username = data.decode("utf-8") data = connection.recv(BUFSIZE) password = data.decode("utf-8") #check UserName case_signUp = checkUsername(username) if case_signUp == 0: #Username is already used return 0 THIS_FOLDER = os.path.dirname(os.path.abspath(__file__)) #deriving the current path of file my_file = os.path.join(THIS_FOLDER, 'data.txt') f = open(my_file,'r') lst=[] num_clients = f.readline() num_clients = int(num_clients) lst.append(str(num_clients)) for i in range(1, num_clients+1): signUp_user = f.readline() signUp_user = signUp_user.strip() lst.append(signUp_user) signUp_pass = f.readline() signUp_pass = signUp_pass.strip() lst.append(signUp_pass) signUp_role = f.readline() signUp_role = signUp_role.strip() lst.append(signUp_role) f.close() lst.append(username) lst.append(password) lst.append(str(0)) fout = open(my_file,'w') lst[0] = str(int(lst[0]) + 1) fout.write(lst[0]) fout.write("\n") for i in range(1, len(lst)): fout.write(lst[i]) fout.write("\n") fout.close() return 1 #Sign Up Succesful if __name__ == "__main__": app = qtw.QApplication(sys.argv) ui = MainServer() sys.exit(app.exec_())