问题python socket.error:[Errno 99]无法分配请求的地址

时间:2016-12-22 22:20:35

标签: python-2.7 sockets

我有问题我不能运行这个脚本 我尝试按如下方式创建ICMP网络嗅探器:

import socket
import os

host = '192.168.1.9'

if os.name == "nt":
   socket_protocol = socket.IPPROTO_IP
else:
   socket_protocol = socket.IPPROTO_ICMP
   sniffer = socket.socket(socket.AF_INET, socket.SOCK_RAW,socket_protocol)
   sniffer.bind((host, 0))   

   sniffer.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1)

if os.name == "nt":

  sniffer.ioctl(socket.SIO_RCVALL, socket.RCVALL_ON)
  print sniffer.recvfrom(656565)

if os.name == "nt":

  sniffer.ioctl(socket.SIO_RCVALL, socket.RCVALL_OFF)



Traceback (most recent call last):
File "exampl.py", line 11, in <module>
sniffer.bind((host, 6000))
File "/usr/lib/python2.7/socket.py", line 228, in meth
return getattr(self._sock,name)(*args)
socket.error: [Errno 99] Cannot assign requested address

我该怎么做才能防止此错误?

1 个答案:

答案 0 :(得分:0)

socket.error: [Errno 99] Cannot assign requested address
     

我该怎么做才能防止此错误?

errno 99的名称定义为

#define EADDRNOTAVAIL   99      /* Cannot assign requested address */

man bind说:

   EADDRNOTAVAIL
          A  nonexistent  interface was requested or the requested address
          was not local.

这建议尝试使用本地地址,并且确实没有更多错误:

host = 'localhost'