def read_interface(self):
        """Read Packets from the packet capture interface"""

        # Spin up the packet capture
        if self._iface_is_file():
            self.pcap = pcapy.open_offline(self.iface_name)
        else:
            try:
                # self.pcap = pcap.pcap(name=self.iface_name, promisc=True, immediate=True)
                #   snaplen (maximum number of bytes to capture _per_packet_)
                #   promiscious mode (1 for true)
                #   timeout (in milliseconds)
                self.pcap = pcapy.open_live(self.iface_name, 65536 , 1 , 0)
            except OSError:
                try:
                    logger.warning('Could not get promisc mode, turning flag off')
                    self.pcap = pcapy.open_live(self.iface_name, 65536 , 0 , 0)
                except OSError:
                    log_utils.panic('Could no open interface with any options (may need to be sudo)')

        # Add the BPF if it's specified
        if self.bpf:
            self.pcap.setfilter(self.bpf)
        print('listening on %s: %s' % (self.iface_name, self.bpf))

        # For each packet in the pcap process the contents
        _packets = 0
        while True:
            # Grab the next header and packet buffer
            header, raw_buf = self.pcap.next()

            # If we don't get a packet header break out of the loop
            if not header:
                break;

            # Extract the timestamp from the header and yield the packet
            seconds, micro_sec = header.getts()
            timestamp = seconds + micro_sec * 10**-6
            yield {'timestamp': timestamp, 'raw_buf': raw_buf, 'packet_num': _packets}
            _packets += 1

            # Is there a max packets set if so break on it
            if self.max_packets and _packets >= self.max_packets:
                break

        # All done so report and raise a StopIteration
        try:
            print('Packet stats: %d  received, %d dropped,  %d dropped by interface' % self.pcap.stats())
        except pcapy.PcapError:
            print('No stats available...')
        raise StopIteration
def read_interface ( self ) : if self . _iface_is_file ( ) : self . pcap = pcapy . open_offline ( self . iface_name ) else : try : self . pcap = pcapy . open_live ( self . iface_name , 65536 , 1 , 0 ) except OSError : try : logger . warning ( 'Could not get promisc mode, turning flag off' ) self . pcap = pcapy . open_live ( self . iface_name , 65536 , 0 , 0 ) except OSError : log_utils . panic ( 'Could no open interface with any options (may need to be sudo)' ) if self . bpf : self . pcap . setfilter ( self . bpf ) print ( 'listening on %s: %s' % ( self . iface_name , self . bpf ) ) _packets = 0 while True : header , raw_buf = self . pcap . next ( ) if not header : break ; seconds , micro_sec = header . getts ( ) timestamp = seconds + micro_sec * 10 ** - 6 yield { 'timestamp' : timestamp , 'raw_buf' : raw_buf , 'packet_num' : _packets } _packets += 1 if self . max_packets and _packets >= self . max_packets : break try : print ( 'Packet stats: %d  received, %d dropped,  %d dropped by interface' % self . pcap . stats ( ) ) except pcapy . PcapError : print ( 'No stats available...' ) raise StopIteration
Read Packets from the packet capture interface