AttackContext.py 829 B

1234567891011121314151617181920212223242526
  1. import tempfile
  2. class AttackContext:
  3. def __init__(self):
  4. # files allocated by attacks, intended to get copied next to the pcap
  5. # the keys are the suffix for later use, e.g. "_extra_info.txt"
  6. # the values are the respectable file names
  7. self.allocated_files = {}
  8. def allocate_file(self, suffix):
  9. if suffix in self.allocated_files:
  10. raise ValueError("File with suffix %s is already allocated" % suffix)
  11. file = tempfile.NamedTemporaryFile(mode = "w", suffix = suffix, delete = False)
  12. self.allocated_files[suffix] = file.name
  13. return file
  14. def reset(self):
  15. self.allocated_files.clear()
  16. def get_allocated_files(self):
  17. return_ = list(self.allocated_files.items())
  18. self.allocated_files.clear()
  19. return return_