当前位置: 首页>>代码示例>>Python>>正文


Python IPAddr.toSignedN方法代码示例

本文整理汇总了Python中pox.lib.addresses.IPAddr.toSignedN方法的典型用法代码示例。如果您正苦于以下问题:Python IPAddr.toSignedN方法的具体用法?Python IPAddr.toSignedN怎么用?Python IPAddr.toSignedN使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在pox.lib.addresses.IPAddr的用法示例。


在下文中一共展示了IPAddr.toSignedN方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: PCap

# 需要导入模块: from pox.lib.addresses import IPAddr [as 别名]
# 或者: from pox.lib.addresses.IPAddr import toSignedN [as 别名]

#.........这里部分代码省略.........
    self.device = None
    self.period = period
    self.netmask = IPAddr("0.0.0.0")
    self._quitting = False
    self.addresses = {}
    if callback is None:
      self.callback = self.__class__._handle_rx
    else:
      self.callback = callback
    if device is not None:
      self.open(device)
    if self.pcap is not None:
      if start:
        self.start()

  def _handle_rx (self, data, sec, usec, length):
    pass

  def open (self, device, promiscuous = None, period = None,
            incoming = True, outgoing = False):
    assert self.device is None
    self.addresses = self.get_devices()[device]['addrs']
    if 'AF_INET' in self.addresses:
      self.netmask = self.addresses['AF_INET'].get('netmask')
      if self.netmask is None: self.netmask = IPAddr("0.0.0.0")
    #print "NM:",self.netmask
    #print self.addresses['AF_LINK']['addr']
    self.device = device
    if period is not None:
      self.period = period
    if promiscuous is not None:
      self.promiscuous = promiscuous
    self.pcap = pcapc.open_live(device, 65535,
                                1 if self.promiscuous else 0, self.period)
    pcapc.setdirection(self.pcap, incoming, outgoing)
    self.packets_received = 0
    self.packets_dropped = 0
    if self.deferred_filter is not None:
      self.set_filter(*self.deferred_filter)
      self.deferred_filter = None

  def set_direction (self, incoming, outgoing):
    pcapc.setdirection(self.pcap, incoming, outgoing)

  def _thread_func (self):
    while not self._quitting:
      pcapc.dispatch(self.pcap,100,self.callback,self)
      self.packets_received,self.packets_dropped = pcapc.stats(self.pcap)

    self._quitting = False
    self._thread = None

  def _handle_GoingDownEvent (self, event):
    self.close()

  def start (self):
    assert self._thread is None
    from pox.core import core
    core.addListeners(self, weak=True)
    self._thread = Thread(target=self._thread_func)
    #self._thread.daemon = True
    self._thread.start()

  def stop (self):
    t = self._thread
    if t is not None:
      self._quitting = True
      pcapc.breakloop(self.pcap)
      t.join()

  def close (self):
    if self.pcap is None: return
    self.stop()
    pcapc.close(self.pcap)
    self.pcap = None

  def __del__ (self):
    self.close()

  def inject (self, data):
    if isinstance(data, pkt.ethernet):
      data = data.pack()
    if not isinstance(data, bytes):
      data = bytes(data) # Give it a try...
    return pcapc.inject(self.pcap, data)

  def set_filter (self, filter, optimize = True):
    if self.pcap is None:
      self.deferred_filter = (filter, optimize)
      return

    if isinstance(filter, str):
      filter = Filter(filter, optimize, self.netmask.toSignedN(),
                      pcap_obj=self)
    elif isinstance(filter, Filter):
      pass
    else:
      raise RuntimeError("Filter must be string or Filter object")

    pcapc.setfilter(self.pcap, filter._pprogram)
开发者ID:09beeihaq,项目名称:pox,代码行数:104,代码来源:__init__.py

示例2: PCap

# 需要导入模块: from pox.lib.addresses import IPAddr [as 别名]
# 或者: from pox.lib.addresses.IPAddr import toSignedN [as 别名]

#.........这里部分代码省略.........
            return

        self.blocking = False

        while not self._quitting:
            rr, ww, xx = select.select(fd, [], fd, 2)

            if xx:
                # Apparently we're done here.
                break
            if rr:
                r = self.next_packet(allow_threads=False)
                if r[-1] == 0:
                    continue
                if r[-1] == 1:
                    self.callback(self, r[0], r[1], r[2], r[3])
                else:
                    break

        self._quitting = False
        self._thread = None

    def _thread_func(self):
        while not self._quitting:
            pcapc.dispatch(self.pcap, 100, self.callback, self, bool(self.use_bytearray), True)
            self.packets_received, self.packets_dropped = pcapc.stats(self.pcap)

        self._quitting = False
        self._thread = None

    def _handle_GoingDownEvent(self, event):
        self.close()

    def start(self):
        assert self._thread is None
        from pox.core import core

        core.addListeners(self, weak=True)

        if self.use_select:
            self._thread = Thread(target=self._select_thread_func)
        else:
            self._thread = Thread(target=self._thread_func)
        # self._thread.daemon = True
        self._thread.start()

    def stop(self):
        t = self._thread
        if t is not None:
            self._quitting = True
            pcapc.breakloop(self.pcap)
            t.join()

    def close(self):
        if self.pcap is None:
            return
        self.stop()
        pcapc.close(self.pcap)
        self.pcap = None

    def __del__(self):
        self.close()

    @property
    def _pcap(self):
        if self.pcap is None:
            raise RuntimeError("PCap object not open")
        return self.pcap

    def inject(self, data):
        if isinstance(data, pkt.ethernet):
            data = data.pack()
        if not isinstance(data, (bytes, bytearray)):
            data = bytes(data)  # Give it a try...
        return pcapc.inject(self.pcap, data)

    def set_filter(self, filter, optimize=True):
        if self.pcap is None:
            self.deferred_filter = (filter, optimize)
            return

        if isinstance(filter, str):
            filter = Filter(filter, optimize, self.netmask.toSignedN(), pcap_obj=self)
        elif isinstance(filter, Filter):
            pass
        else:
            raise RuntimeError("Filter must be string or Filter object")

        pcapc.setfilter(self.pcap, filter._pprogram)

    def fileno(self):
        if self.pcap is None:
            raise RuntimeError("PCap object not open")
        r = pcapc.get_selectable_fd(self.pcap)
        if r == -1:
            raise RuntimeError("Selectable FD not available")
        return r

    def __str__(self):
        return "PCap(device=%s)" % (self.device)
开发者ID:bhushan99,项目名称:Software-defined-networking,代码行数:104,代码来源:__init__.py


注:本文中的pox.lib.addresses.IPAddr.toSignedN方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。