MITM Proxy – intercept & Modify https content through python script

  https, mitmproxy, proxy, python

I’m trying to intercept and modify https content using Mitm Proxy.

It works really well using the GUI but I’d like to use a python script.

I tried this script:

from mitmproxy import http

def request(flow: http.HTTPFlow) -> None:
    if flow.request.pretty_url == "":
        flow.response = http.HTTPResponse.make(
            200,  # (optional) status code
            b"Hello World, this is a test",  # (optional) content
            {"Content-Type": "text/html"}  # (optional) headers

def response(flow: http.HTTPFlow):

    if flow.response.raw_content and flow.request.pretty_url == "":
        file = open(b"C:UsersmyuserPycharmProjectsmitmProxdata.txt", "a")
        file.write(str(flow.response.) + "n")

However, the content I intercept is encrypted and not in clear despite the fact that the content is clear on the web GUI !

Does anyone have an idea why my script intercepts encrypted content and the web GUI prints the clear data?

Source: Python Questions