def main(): args = argparser() RxP.listen(args.X) print("Listening for connections...") conn = RxP.accept() # s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # s.bind(('127.0.0.1', args.X)) # s.listen(5) # (clientsocket, address) = s.accept() # parser = Command_Parser(args.X, args.A, args.P, 1, clientsocket) parser = Command_Parser(args.X, args.A, args.P, conn) print("Connection established!") running = True while running: cmd = input("Command: ") running = parser.parse_command(cmd)
def parse_command(self, inp): """Parses a command Params: inp - command input (string) """ inp_list = inp.split(' ') if len(inp_list) > 2: print("Invalid syntax") cmd = inp_list[0].lower() param = inp_list[1] if len(inp_list) > 1 else None if self.type == CLIENT: cmds = ["get", "post", "connect", "window", "disconnect"] if cmd != "connect" and not self.conn and cmd != "disconnect": print("You need to connect first!") elif cmd == "connect": print("Connecting...") self.conn = RxP.connect((self.A, self.P), self.X) if self.conn: print("Connected!") self.start_conn_thread() else: print("Connection Failed") elif cmd == "window": try: win = int(param) self.cmd_queue.put(inp) except: print("Invalid window size!") elif cmd == "disconnect": print("Attempting to close the connection") self.cmd_queue.put(inp) for i in range(5): if not self.cmd_queue.empty(): time.sleep(1) if not self.cmd_queue.empty(): print("Server took too long to close gracefully, exiting...") return False elif cmd == "get" or cmd == "post": print(cmd + " for file: " + param) msg = cmd + " " + param self.cmd_queue.put(msg) else: print("Invalid command: " + cmd) print("Valid commands: ") for c in cmds: print(c, end=' ') print("") return True elif self.type == SERVER: cmds = ["window", "terminate"] if cmd == "window": try: win = int(param) self.cmd_queue.put(inp) except: print("Invalid window size!") elif cmd == "terminate": print("Attempting to close the connection") self.cmd_queue.put(inp) for i in range(5): if not self.cmd_queue.empty(): time.sleep(1) if not self.cmd_queue.empty(): print("Client took too long to close gracefully, exiting...") return False return True