Skip to content

Commit

Permalink
Remove ping
Browse files Browse the repository at this point in the history
Ref #12
  • Loading branch information
Ircama committed Aug 30, 2024
1 parent 1141849 commit 2d81571
Showing 1 changed file with 21 additions and 18 deletions.
39 changes: 21 additions & 18 deletions find_printers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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):
Expand Down

0 comments on commit 2d81571

Please sign in to comment.