如何在 "def request()" return 中直接响应

How to in "def request()" return the response directly

如标题,我想在“def request()”中处理数据,并return直接响应;

我不想流经目标服务器;

这种方式可行吗?谢谢!!!

这是一个关于如何做到这一点的例子:

"""Send a reply from the proxy without sending any data to the remote server."""
from mitmproxy import http


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

来源:https://github.com/mitmproxy/mitmproxy/blob/main/examples/addons/http-reply-from-proxy.py