From 2d81571e32aecd76a996bceed8dbfd3e7a8c7d04 Mon Sep 17 00:00:00 2001 From: Ircama Date: Fri, 30 Aug 2024 11:48:57 +0200 Subject: [PATCH] Remove ping Ref #12 --- find_printers.py | 39 +++++++++++++++++++++------------------ 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/find_printers.py b/find_printers.py index dd6819c..33cff03 100644 --- a/find_printers.py +++ b/find_printers.py @@ -6,18 +6,14 @@ from epson_print_conf import EpsonPrinter - # suppress pysnmp warnings warnings.filterwarnings("ignore", category=SyntaxWarning) # common printer ports PRINTER_PORTS = [9100, 515, 631] -class PrinterScanner: - def ping(self, host): - result = subprocess.run(['ping', '-n', '1', host], stdout=subprocess.PIPE, creationflags=subprocess.CREATE_NO_WINDOW) - return 'Reply from' in result.stdout.decode('utf-8') +class PrinterScanner: def check_printer(self, ip, port): try: @@ -38,19 +34,26 @@ def get_printer_name(self, ip): return None def scan_ip(self, ip): - if self.ping(ip): - for port in PRINTER_PORTS: - if self.check_printer(ip, port): - try: - hostname = socket.gethostbyaddr(ip)[0] - except socket.herror: - hostname = "Unknown" - - printer_name = self.get_printer_name(ip) - if printer_name: - return {"ip": ip, "hostname": hostname, "name": printer_name} - else: - return {"ip": ip, "hostname": hostname, "name": "Unknown"} + for port in PRINTER_PORTS: + if self.check_printer(ip, port): + try: + hostname = socket.gethostbyaddr(ip)[0] + except socket.herror: + hostname = "Unknown" + + printer_name = self.get_printer_name(ip) + if printer_name: + return { + "ip": ip, + "hostname": hostname, + "name": printer_name + } + else: + return { + "ip": ip, + "hostname": hostname, + "name": "Unknown" + } return None def get_all_printers(self, ip_addr="", local=False):