Skip to content

Commit

Permalink
Add simple example for rsdos reading
Browse files Browse the repository at this point in the history
  • Loading branch information
salcock committed Apr 22, 2021
1 parent d011b5f commit 439f952
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions examples/rsdos-example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Example code that uses the AvroRsdosReader extension class to count
# DOS attacks via a perDos callback method

import sys
from pyavro_stardust.rsdos import AvroRsdosReader, RsdosAttribute, \
AvroRsdos

count = 0
attack_pkts = 0

def perDosCallback(rsdos, userarg):
global count, attack_pkts

count += 1
dos = rsdos.asDict()
attack_pkts += dos['packet_count']

# Ideally, we'd do things with the other fields in 'dos' as well,
# but this is just intended to be a very simple example

def run():
# sys.argv[1] must be a valid wandio path -- e.g. a swift URL or
# a path to a file on disk
reader = AvroRsdosReader(sys.argv[1])
reader.start()

# This will read all of the attack records and call `perDosCallback` on
# each one
reader.perAvroRecord(perDosCallback)
reader.close()

# Display our final results
print("Attacks", count, " Packets:", attack_pkts)

run()

0 comments on commit 439f952

Please sign in to comment.