mitmproxy/examples/upsidedownternet.py

16 lines
670 B
Python
Raw Normal View History

2014-01-04 02:15:08 +00:00
import cStringIO
from PIL import Image
2014-09-08 14:02:31 +00:00
from libmproxy.protocol.http import decoded
2014-01-04 02:15:08 +00:00
2014-09-08 14:02:31 +00:00
def response(context, flow):
if flow.response.headers.get_first("content-type", "").startswith("image"):
with decoded(flow.response): # automatically decode gzipped responses.
try:
s = cStringIO.StringIO(flow.response.content)
img = Image.open(s).rotate(180)
s2 = cStringIO.StringIO()
img.save(s2, "png")
flow.response.content = s2.getvalue()
flow.response.headers["content-type"] = ["image/png"]
except: # Unknown image types etc.
pass