From e4f80f409e8852a5cddae8bc535b79aec397baa4 Mon Sep 17 00:00:00 2001 From: Brian Wylie Date: Mon, 21 Dec 2020 09:51:16 -0700 Subject: [PATCH] removing StopIteration; fixing some flake8 --- chains/sources/packet_streamer.py | 12 +++++++----- examples/__init__.py | 0 examples/simple_packet_print.py | 12 ++++++++---- 3 files changed, 15 insertions(+), 9 deletions(-) delete mode 100644 examples/__init__.py mode change 100755 => 100644 examples/simple_packet_print.py diff --git a/chains/sources/packet_streamer.py b/chains/sources/packet_streamer.py index bc3324f..6424bba 100644 --- a/chains/sources/packet_streamer.py +++ b/chains/sources/packet_streamer.py @@ -8,6 +8,7 @@ from chains.utils import file_utils, log_utils logger = log_utils.get_logger() + class PacketStreamer(source.Source): """Stream out the packets from the given network interface @@ -73,11 +74,11 @@ def read_interface(self): # 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) + 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) + 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)') @@ -94,7 +95,7 @@ def read_interface(self): # If we don't get a packet header break out of the loop if not header: - break; + break # Extract the timestamp from the header and yield the packet seconds, micro_sec = header.getts() @@ -106,12 +107,12 @@ def read_interface(self): if self.max_packets and _packets >= self.max_packets: break - # All done so report and raise a StopIteration + # All done so print out a small report 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 test(): """Open up a test pcap file and stream the packets""" @@ -127,5 +128,6 @@ def test(): for packet in streamer.output_stream: print(packet) + if __name__ == '__main__': test() diff --git a/examples/__init__.py b/examples/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/examples/simple_packet_print.py b/examples/simple_packet_print.py old mode 100755 new mode 100644 index 8369c0b..7c98758 --- a/examples/simple_packet_print.py +++ b/examples/simple_packet_print.py @@ -12,6 +12,7 @@ from chains.links import packet_meta, reverse_dns, transport_meta from chains.sinks import packet_printer, packet_summary + def run(iface_name=None, bpf=None, summary=None, max_packets=100): """Run the Simple Packet Printer Example""" @@ -34,6 +35,7 @@ def run(iface_name=None, bpf=None, summary=None, max_packets=100): # Pull the chain printer.pull() + def test(): """Test the Simple Packet Printer Example""" from chains.utils import file_utils @@ -41,21 +43,23 @@ def test(): # For the test we grab a file, but if you don't specify a # it will grab from the first active interface data_path = file_utils.relative_dir(__file__, '../data/http.pcap') - run(iface_name = data_path) + run(iface_name=data_path) + def my_exit(): """Exit on Signal""" print('Goodbye...') sys.exit() + if __name__ == '__main__': # Collect args from the command line parser = argparse.ArgumentParser() parser.add_argument('-bpf', type=str, help='BPF Filter for PacketStream Class') - parser.add_argument('-s','--summary', action="store_true", help='Summary instead of full packet print') - parser.add_argument('-m','--max-packets', type=int, default=100, help='How many packets to process (0 for infinity)') - parser.add_argument('-p','--pcap', type=str, help='Specify a pcap file instead of reading from live network interface') + parser.add_argument('-s', '--summary', action="store_true", help='Summary instead of full packet print') + parser.add_argument('-m', '--max-packets', type=int, default=100, help='How many packets to process (0 for infinity)') + parser.add_argument('-p', '--pcap', type=str, help='Specify a pcap file instead of reading from live network interface') args, commands = parser.parse_known_args() if commands: print('Unrecognized args: %s' % commands)