def add_computer(self, computer): if isinstance(computer, Computer): if re.match(MAC_REGULAR_EXPRESSION, computer.mac): if re.match(IP_REGULAR_EXPRESSION, computer.ip): if NetMap.can_ip_in_my_network(computer.ip): computer.active = False self.__database.add_row(computer) return "Computer successfully added." return "The IP address entered cannot exist in this network" return "Invalid IP format" return "Invalid MAC format"
def start(self): """ Starts the server """ if self.starting or self.running: raise NameError('Already running or starting...') self.starting = True self.__signature = Cipher.crate_signature() self.__public_key = Cipher() self.__print("Updating database...") pythoncom.CoInitialize() current_arp = NetMap.map() pythoncom.CoUninitialize() database = self.__database.read() # Loop for updating the state of the computer for computer in database: if computer.ip == gethostbyname(gethostname()): computer.active = True self.__database.update_state(computer) elif computer not in current_arp: computer.active = False self.__database.update_state(computer) # Loop for updating the database for computer in current_arp: if computer not in database: self.__database.add_row(computer) database = self.__database.read() self.__print("Database updated.") if not exists(DOWNLOAD_UPLOAD): makedirs(DOWNLOAD_UPLOAD) network_scan_thread = Thread(target=self.__network_scan) network_scan_thread.setDaemon(True) network_scan_thread.start() broadcast_announce_thread = Thread(target=self.__broadcast_announce) broadcast_announce_thread.setDaemon(True) broadcast_announce_thread.start() self.__main_socket.bind(("0.0.0.0", SERVER_PORT)) self.__main_socket.listen(1) self.starting = False self.running = True self.__run()
def __network_scan(self): """ Scans the network """ while True: sleep(NET_SCAN_WAIT) pythoncom.CoInitialize() current_arp = NetMap.map() pythoncom.CoUninitialize() database = self.__database.read() for computer in database: if computer.ip == gethostbyname(gethostname()): computer.active = True self.__database.update_state(computer) elif computer not in current_arp: computer.active = False self.__database.update_state(computer) for computer in current_arp: if computer not in database: self.__database.add_row(computer) database = self.__database.read()