0

When run below code to detect if a interface with ip exist, it cost 4.5 secs on my windows machine!

import socket
import timeit

def ip_exist(host):
    try:
        socket.gethostbyaddr(host)
        return True
    except socket.herror:
        return False
        pass

start_time = timeit.default_timer()
retval = ip_exist("192.168.128.1")  
print(retval)
print(timeit.default_timer() - start_time)

I wish the solution work for both windows and linux. please help share right way to do it!

lucky1928
  • 8,708
  • 10
  • 43
  • 92
  • Have you tried https://stackoverflow.com/questions/17915094/faster-or-batch-alternative-to-pythons-socket-gethostbyaddr ? – Mihai Apr 10 '21 at 19:42
  • I personally use `pythonping` package to check if interface is up, it has almost no delay, and the trick is to set `timeout` so your interface doesnt spend too much time on that. – Bijay Regmi Apr 10 '21 at 19:59

0 Answers0