redis-py与套接字冲突并引发套接字10043错误

时间:2016-01-11 15:15:12

标签: python sockets redis redis-py

我想写一个程序来监控主机是否处于活动状态。我们可以设置我们要监控哪个主机的ip段。

现在我使用套接字发送icmp数据包,以便我们可以判断主机是否处于活动状态。我使用redis进行缓存。我已经尝试了很多。我得到了一些收益,所以我当然可以说redis- py模块与socket冲突。但是,我不知道要解决这个问题。

相关代码如下:

class icmpThr(threading.Thread):
  def __init__(self, ipPool, timeout=3, isV6=False):
    threading.Thread.__init__(self)
    self.timeout = timeout
    self.isV6 = isV6

    self.ipPool = ipPool
    self.__id = os.getpid()
    self.__data = struct.pack('d', 0)

  def __checkSum(self, packet):
    cksum = 0
    if len(packet) & 1:
      packet = packet + '\0'
    words = array.array('h', packet)
    for word in words:
      cksum += (word & 0xffff)

    cksum = (cksum >> 16) + (cksum & 0xffff)
    cksum = (cksum >> 16) + (cksum & 0xffff)

    return (~cksum) & 0xffff

  @property
  def __icmpSocket(self):
    if self.isV6:
      Sock = socket.socket(socket.AF_INET6, socket.SOCK_RAW, socket.getprotobyname("ipv6-icmp"))
    else:
      Sock = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.getprotobyname("icmp"))   #the error raised here
    return Sock

  @property
  def __icmpPacket(self):
    if self.isV6:
      header = struct.pack('BbHHh', 128, 0, 0, self.__id, 0)
    else:
      header = struct.pack('bbHHh', 8, 0, 0, self.__id, 0)
    packet = header + self.__data
    cksum = self.__checkSum(packet)

    if self.isV6:
      header = struct.pack('BbHHh', 128, 0, 0, self.__id, 0)
    else: 
      header = struct.pack('bbHHh', 8, 0, cksum, self.__id, 0)
    return header + self.__data

  def run(self):
    sock = self.__icmpSocket
    packet = self.__icmpPacket

    send = sendThr(packet, self.ipPool, sock)
    recv = recvThr(sock, self.ipPool, send)

    send.start()
    recv.start()

def icmpScan():
  ipDb = redis.Redis(host='localhost',port=6379,db=0)

  ipPool = set(['10.0.0.1'])
  ipDb.set('ip_pool', ipPool)#when I delete this line, it runs well

  icmp = icmpThr(ipPool)

  icmp.start()
  icmp.join()

还有什么,我被搜查了错误10043.这意味着:协议不受支持。

enter image description here

0 个答案:

没有答案