0

I'm doing a project using python and I'm trying the filter an HAR file and copy it into another readable file. For the HAR file to be readable, the first few lines from the original file must be copied to the output file. However when I try to copy the first 8 lines from the original file to the output file, 8 blank lines are showing in the output file and therefore not the content that I need. Also note that I am eventually filtering the original HAR file the show me the entries where the content-type is application/json

This is my code below :

    import json 
import sys
from haralyzer import HarParser 

number_of_lines = 8

#opening original har file as read 
with open('testing.har', 'r', encoding="utf8") as original_file:
    har_parser = HarParser(json.loads(original_file.read()))

    #opening the har file I want to copy into 
    with open('out.har', 'w') as output_file:

        original_stdout = sys.stdout
        sys.stdout = output_file

        #copying this first 8 lines from the original file to the output file 
        for i in range(number_of_lines):
                line = original_file.readline()
                print(line)

        #matching the entires that have content type application/json and copying them to the output file.
        for page in har_parser.pages:
            for entry in page.entries:
                if har_parser.match_headers(entry,'response','Content-Type','application/json'):
                    print(entry)
        
        sys.stdout = original_stdout

Does anyone have any idea what the problem is ? I can also send the original HAR file as well. TIA

Angele Aquilina
  • 3
  • 1
  • 3
  • 9

0 Answers0