Python TypeError: 'send_arp_reply() got an unexpected keyword argument 'timeout'' 解决方法

在Python代码中,您可能会遇到以下错误:

return self.func(*args)
  File "h.py", line 230, in save_button_click
    save_records()
  File "h.py", line 175, in save_records
    active_hosts = scan_network(ifname)
  File "h.py", line 168, in scan_network
    response = send_arp_reply(ifname, timeout=1)
TypeError: send_arp_reply() got an unexpected keyword argument 'timeout'

这个错误意味着您在调用 send_arp_reply 函数时传递了一个名为 timeout 的关键字参数,但该函数定义中并没有接收 timeout 参数。

解决方法

您可以尝试修改 send_arp_reply 函数的定义,将 timeout 参数改为可选参数(即使用默认值),如下所示:

def send_arp_reply(ifname, dst_ip, src_mac=None, src_ip=None, timeout=None):
    # ...

然后在函数内部根据 timeout 是否为 None 来决定是否使用超时参数,如下所示:

def send_arp_reply(ifname, dst_ip, src_mac=None, src_ip=None, timeout=None):
    # ...
    if timeout is not None:
        response, _ = srp(arp_request, iface=ifname, timeout=timeout)
    else:
        response, _ = srp(arp_request, iface=ifname)
    # ...

这样修改后,调用 send_arp_reply 函数时可以不传递 timeout 参数,即使用默认值,也可以传递 timeout 参数来指定超时时间。

Python TypeError: send_arp_reply() got an unexpected keyword argument 'timeout' 解决方法

原文地址: https://www.cveoy.top/t/topic/jm8h 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录