![python 中的“socket.error: [Errno 99] 无法分配请求的地址”](https://linux22.com/image/62390/python%20%E4%B8%AD%E7%9A%84%E2%80%9Csocket.error%3A%20%5BErrno%2099%5D%20%E6%97%A0%E6%B3%95%E5%88%86%E9%85%8D%E8%AF%B7%E6%B1%82%E7%9A%84%E5%9C%B0%E5%9D%80%E2%80%9D.png)
我尝试创建 ICMP 网络嗅探器,如下所示:
import socket,os
host="192.168.1.7"
#create a socket protocol
socket_protocol=socket.IPPROTO_ICMP
sniffer = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket_protocol)
sniffer.bind((host, 6677))
sniffer.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1)
print(sniffer.recvfrom(65565))
“192.168.1.7”是连接到我的无线网络接口所连接的路由器的另一台PC的本地地址。我通过wireshark捕获获得了这个地址。
执行代码时,我收到以下错误
Traceback (most recent call last):
File "/root/test.py", line 18, in <module>
sniffer.bind((host, 6677))
socket.error: [Errno 99] Cannot assign requested address
当我使用我的无线网卡接口本地IP地址(“192.168.1.9”)时,一切正常。
我该怎么做才能防止这个错误...提前致谢!