mitmproxy/examples/addons/io-write-flow-file.py
Daniel Karandikar b4f49d0186
Update io-write-flow-file.py example with option (#6464)
#### Description

Update example addon as described in
https://github.com/mitmproxy/mitmproxy/issues/6445

#### Checklist

 - [x] I have updated tests where applicable.

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Co-authored-by: Maximilian Hils <git@maximilianhils.com>
2024-03-06 21:18:43 +00:00

35 lines
972 B
Python

"""
Generate a mitmproxy dump file.
This script demonstrates how to generate a mitmproxy dump file,
as it would also be generated by passing `-w` to mitmproxy.
In contrast to `-w`, this gives you full control over which
flows should be saved and also allows you to rotate files or log
to multiple files in parallel.
"""
import os
import random
from typing import BinaryIO
from mitmproxy import http
from mitmproxy import io
class Writer:
def __init__(self) -> None:
# We are using an environment variable to keep the example as simple as possible,
# consider implementing this as a mitmproxy option instead.
filename = os.getenv("MITMPROXY_OUTFILE", "out.mitm")
self.f: BinaryIO = open(filename, "wb")
self.w = io.FlowWriter(self.f)
def response(self, flow: http.HTTPFlow) -> None:
if random.choice([True, False]):
self.w.add(flow)
def done(self):
self.f.close()
addons = [Writer()]