1- from mitmproxy import http
1+ from mitmproxy import http , ctx
22import time
3- import sys
43import csv
54import os
65
76# Initialize a list to track request details
87requests_data = []
98
10- # Get the output file path from command-line arguments
11- output_file_path = sys .argv [3 ] if len (sys .argv ) > 3 else 'default_output.csv'
12- print (f"OUTPUT: { output_file_path } " )
139
14- # Create a CSV file and write headers if the file does not exist
15- if not os .path .exists (output_file_path ):
16- with open (output_file_path , 'w' , newline = '' ) as csvfile :
17- writer = csv .writer (csvfile )
18- writer .writerow (["url" , "status_code" , "duration in seconds" , "content_length in bytes" , "timestamp" ]) # Write CSV header
10+ def load (l ):
11+ # Define the custom option `output_file_path`
12+ ctx .options .add_option ("output_file_path" , str , "" , "Path to output CSV file" )
13+
1914
2015def request (flow : http .HTTPFlow ) -> None :
2116 # Capture request data when it's made
@@ -30,6 +25,12 @@ def request(flow: http.HTTPFlow) -> None:
3025 })
3126
3227def response (flow : http .HTTPFlow ) -> None :
28+ output_file_path = ctx .options .output_file_path
29+ create_file_if_not_exists (output_file_path )
30+
31+ # print("Flow", flow)
32+ # print("Response", flow.response)
33+
3334 res = flow .response
3435 end_time = time .time ()
3536
@@ -58,3 +59,23 @@ def response(flow: http.HTTPFlow) -> None:
5859
5960 # Optionally print captured details for console output
6061 print (f"Captured: { captured_details } " )
62+
63+ def create_file_if_not_exists (filepath ):
64+ """
65+ Check if the output CSV file exists.
66+ If it does not exist, create the file and add csv headers.
67+ """
68+
69+ if not os .path .exists (filepath ):
70+ with open (filepath , "w" , newline = "" ) as csvfile :
71+ writer = csv .writer (csvfile )
72+ writer .writerow (
73+ [
74+ "url" ,
75+ "status_code" ,
76+ "duration_in_seconds" ,
77+ "content_length_in_bytes" ,
78+ "timestamp" ,
79+ ]
80+ )
81+ print (f"Created output file: { filepath } " )
0 commit comments