Last active
March 31, 2023 12:13
-
-
Save jasonmfehr/6ec6419f6229f7549f82b5b87ee2f658 to your computer and use it in GitHub Desktop.
Writes mitmproxy data in CSV format to a file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import mitmproxy | |
from datetime import datetime | |
import math | |
class RequestsToCSV: | |
def load(self, loader): | |
# | |
# note: update this path to change the data file name and/or location | |
# | |
self.file_handle = open("requests-" + datetime.now().isoformat().split(".")[0] + ".csv", "w") | |
self.file_handle.write("Request,Start Time,EndTime,Request Method,Path,Response Size Raw (bytes),Response Size Uncompressed (bytes),Duration (ms)\n") | |
self.file_handle.flush() | |
self.request_count = 0 | |
def done(self): | |
self.file_handle.close() | |
def response(self, flow: mitmproxy.http.HTTPFlow): | |
start_time = datetime.fromtimestamp(flow.request.timestamp_start).isoformat() | |
end_time = datetime.fromtimestamp(flow.response.timestamp_end).isoformat() | |
duration = str((flow.response.timestamp_end - flow.request.timestamp_start) * 1000).split(".")[0] | |
self.request_count += 1 | |
self.file_handle.write(str(self.request_count) + "," + start_time + "," + end_time + "," + flow.request.method + "," + flow.request.path + "," + str(len(flow.response.raw_content)) + "," + str(len(flow.response.content)) + "," + str(duration) + "\n") | |
self.file_handle.flush() | |
addons = [ | |
RequestsToCSV() | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This script works with mitmproxy to output http request details to a csv file as the requests flow through the proxy.
To use it, pass the script name to one of the mitmproxy commands using the
-s
flag. For example: