Save cuts to file. If there are multiple flows or cuts, the format is UTF-8 encoded CSV. If there is exactly one row and one column, the data is written to file as-is, with raw bytes preserved. If the path is prefixed with a "+", values are appended if there is an
(
self,
flows: Sequence[flow.Flow],
cuts: mitmproxy.types.CutSpec,
path: mitmproxy.types.Path,
)
| 103 | |
| 104 | @command.command("cut.save") |
| 105 | def save( |
| 106 | self, |
| 107 | flows: Sequence[flow.Flow], |
| 108 | cuts: mitmproxy.types.CutSpec, |
| 109 | path: mitmproxy.types.Path, |
| 110 | ) -> None: |
| 111 | """ |
| 112 | Save cuts to file. If there are multiple flows or cuts, the format |
| 113 | is UTF-8 encoded CSV. If there is exactly one row and one column, |
| 114 | the data is written to file as-is, with raw bytes preserved. If the |
| 115 | path is prefixed with a "+", values are appended if there is an |
| 116 | existing file. |
| 117 | """ |
| 118 | append = False |
| 119 | if path.startswith("+"): |
| 120 | append = True |
| 121 | epath = os.path.expanduser(path[1:]) |
| 122 | path = mitmproxy.types.Path(epath) |
| 123 | try: |
| 124 | if len(cuts) == 1 and len(flows) == 1: |
| 125 | with open(path, "ab" if append else "wb") as fp: |
| 126 | if fp.tell() > 0: |
| 127 | # We're appending to a file that already exists and has content |
| 128 | fp.write(b"\n") |
| 129 | v = extract(cuts[0], flows[0]) |
| 130 | if isinstance(v, bytes): |
| 131 | fp.write(v) |
| 132 | else: |
| 133 | fp.write(v.encode("utf8")) |
| 134 | logger.log(ALERT, "Saved single cut.") |
| 135 | else: |
| 136 | with open( |
| 137 | path, "a" if append else "w", newline="", encoding="utf8" |
| 138 | ) as tfp: |
| 139 | writer = csv.writer(tfp) |
| 140 | for f in flows: |
| 141 | vals = [extract_str(c, f) for c in cuts] |
| 142 | writer.writerow(vals) |
| 143 | logger.log( |
| 144 | ALERT, |
| 145 | "Saved %s cuts over %d flows as CSV." % (len(cuts), len(flows)), |
| 146 | ) |
| 147 | except OSError as e: |
| 148 | logger.error(str(e)) |
| 149 | |
| 150 | @command.command("cut.clip") |
| 151 | def clip( |