使用Scapy实现Ping功能时遇TypeError错误如何修复?
修复Scapy Ping扫描中的
TypeError: IP() got an unexpected keyword argument 'dst'错误 错误原因
这个错误说明你调用的IP()类不支持dst关键字参数,通常由以下情况导致:
- 安装了非官方Scapy分支(如
scapy-python3、scapy3k),这类分支的API与官方版本不一致 - 代码存在命名冲突(比如自定义了名为
IP的变量/函数,覆盖了Scapy的IP类) - 使用了过旧的Scapy版本
修复步骤
1. 安装/升级官方Scapy包
先卸载可能存在的非官方分支,再安装最新官方版本:
pip uninstall -y scapy-python3 scapy3k pip install --upgrade scapy
2. 明确导入Scapy网络层类
修改导入语句,直接指定需要的类,避免命名冲突:
from scapy.layers.inet import IP, ICMP from scapy.all import sr1, conf TIMEOUT = 2 conf.verb = 0 for ip in range(0, 256): target_ip = f"192.168.0.{ip}" packet = IP(dst=target_ip, ttl=20) / ICMP() reply = sr1(packet, timeout=TIMEOUT) if reply: print(f"{reply.dst} is online") else: print(f"Timeout waiting for {target_ip}")
3. 排查命名冲突
检查代码上下文,确认没有自定义名为IP的变量、函数或类——这类命名会覆盖Scapy导入的IP类,导致参数不匹配。
内容的提问来源于stack exchange,提问作者user21501105
相关产品推荐
相关产品推荐

