Python 3.x python3-检测ip存在花费太多时间

Python 3.x python3-检测ip存在花费太多时间,python-3.x,Python 3.x,当运行下面的代码来检测是否存在ip接口时,在我的windows机器上花费4.5秒 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()

当运行下面的代码来检测是否存在ip接口时,在我的windows机器上花费4.5秒

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)

我希望这个解决方案能同时适用于windows和linux。请帮助分享正确的方法做它

你试过了吗?我个人使用
pythonping
包来检查接口是否启动,它几乎没有延迟,诀窍是设置
timeout
,这样你的接口就不会在上面花费太多时间。