aboutsummaryrefslogtreecommitdiffstats
path: root/examples/har_extractor.py
diff options
context:
space:
mode:
authorMaximilian Hils <git@maximilianhils.com>2016-07-15 23:17:57 -0700
committerMaximilian Hils <git@maximilianhils.com>2016-07-15 23:17:57 -0700
commit3602fd7a36d963311339ab11ed36ff00df860f71 (patch)
tree5834a98b35c02639c876544bc645f205068fac99 /examples/har_extractor.py
parenta3c7c84d49c3e6563e7f37ef60c989f99ed96788 (diff)
parent17305643bc482c0b185eec5c64d506790cd26587 (diff)
downloadmitmproxy-3602fd7a36d963311339ab11ed36ff00df860f71.tar.gz
mitmproxy-3602fd7a36d963311339ab11ed36ff00df860f71.tar.bz2
mitmproxy-3602fd7a36d963311339ab11ed36ff00df860f71.zip
Merge remote-tracking branch 'origin/master' into message-body-encoding
Diffstat (limited to 'examples/har_extractor.py')
-rw-r--r--examples/har_extractor.py31
1 files changed, 23 insertions, 8 deletions
diff --git a/examples/har_extractor.py b/examples/har_extractor.py
index 54aa84d3..76059d8e 100644
--- a/examples/har_extractor.py
+++ b/examples/har_extractor.py
@@ -2,6 +2,7 @@
This inline script utilizes harparser.HAR from
https://github.com/JustusW/harparser to generate a HAR log object.
"""
+import mitmproxy.ctx
import six
import sys
import pytz
@@ -54,12 +55,24 @@ class _HARLog(HAR.log):
return self.__page_list__
-def start(context):
+class Context(object):
+ pass
+
+context = Context()
+
+
+def start():
"""
On start we create a HARLog instance. You will have to adapt this to
suit your actual needs of HAR generation. As it will probably be
necessary to cluster logs by IPs or reset them from time to time.
"""
+ if sys.version_info >= (3, 0):
+ raise RuntimeError(
+ "har_extractor.py does not work on Python 3. "
+ "Please check out https://github.com/mitmproxy/mitmproxy/issues/1320 "
+ "if you want to help making this work again."
+ )
context.dump_file = None
if len(sys.argv) > 1:
context.dump_file = sys.argv[1]
@@ -73,7 +86,7 @@ def start(context):
context.seen_server = set()
-def response(context, flow):
+def response(flow):
"""
Called when a server response has been received. At the time of this
message both a request and a response are present and completely done.
@@ -195,7 +208,7 @@ def response(context, flow):
context.HARLog.add(entry)
-def done(context):
+def done():
"""
Called once on script shutdown, after any other events.
"""
@@ -206,17 +219,19 @@ def done(context):
compressed_json_dump = context.HARLog.compress()
if context.dump_file == '-':
- context.log(pprint.pformat(json.loads(json_dump)))
+ mitmproxy.ctx.log(pprint.pformat(json.loads(json_dump)))
elif context.dump_file.endswith('.zhar'):
- file(context.dump_file, "w").write(compressed_json_dump)
+ with open(context.dump_file, "wb") as f:
+ f.write(compressed_json_dump)
else:
- file(context.dump_file, "w").write(json_dump)
- context.log(
+ with open(context.dump_file, "wb") as f:
+ f.write(json_dump)
+ mitmproxy.ctx.log(
"HAR log finished with %s bytes (%s bytes compressed)" % (
len(json_dump), len(compressed_json_dump)
)
)
- context.log(
+ mitmproxy.ctx.log(
"Compression rate is %s%%" % str(
100. * len(compressed_json_dump) / len(json_dump)
)