mitmproxy/examples/addons/commands-paths.py
Maximilian Hils 08895e9ba6 restructure examples
- restructure examples (fix #4031)
 - remove example dependencies from setup.py,
   we do not need special dependencies for our supported addons.
 - unify how we generate docs from code
 - improve example docs
2020-06-23 16:00:14 +02:00

31 lines
693 B
Python

"""Handle file paths as command arguments."""
import typing
from mitmproxy import command
from mitmproxy import ctx
from mitmproxy import flow
from mitmproxy import types
class MyAddon:
@command.command("myaddon.histogram")
def histogram(
self,
flows: typing.Sequence[flow.Flow],
path: types.Path,
) -> None:
totals = {}
for f in flows:
totals[f.request.host] = totals.setdefault(f.request.host, 0) + 1
with open(path, "w+") as fp:
for cnt, dom in sorted([(v, k) for (k, v) in totals.items()]):
fp.write("%s: %s\n" % (cnt, dom))
ctx.log.alert("done")
addons = [
MyAddon()
]