使用 mitmproxy 返回自定义响应

Wär*_*ing 1 mitmproxy

当使用 mitmproxy 和以下代码时,我尝试返回自定义响应

from libmproxy.models import HTTPResponse
from netlib.http import Headers

def request(context, flow):
    if flow.request.pretty_url.endswith("example.com/index.php"):
        resp = HTTPResponse(
            [1, 1], 200, "OK",
            Headers(Content_Type="text/html"),
            "<html><body>hello world</body></html>")
        flow.reply(resp)
Run Code Online (Sandbox Code Playgroud)

但结果并不符合预期,在浏览器中它显示为 http 状态代码、标头和正文为明文

[1,1] 200 OK
Content-Type: text/html

<html><body>hello world</body></html>
Run Code Online (Sandbox Code Playgroud)

我怎样才能将它作为实际的“http”响应返回,以便chrome将其渲染为html?

xli*_*iiv 7

该片段应如下所示

from mitmproxy import http


def request(flow):
    if flow.request.pretty_url.endswith("example.com/index.php"):
        flow.response = http.HTTPResponse.make(
            200,
            "<html><body>hello world</body></html>",
            {"content-type":"text/html"},
        )

Run Code Online (Sandbox Code Playgroud)

对于较新版本的mitmproxy.