1

使用带有以下代码的 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)

但是结果并不像预期的那样,在浏览器中它显示为 http 状态代码,标题 och 正文为明文

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

<html><body>hello world</body></html>

如何将其作为实际的“http”响应返回,以便 chrome 将其呈现为 html?

4

3 回答 3

3

片段应如下所示

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"},
        )

对于较新版本的mitmproxy.

于 2019-06-08T21:40:14.833 回答
2

不要以 http 版本为例。将 [1, 1] 替换为“HTTP/1.1”

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(
            "HTTP/1.1", 200, "OK",
            Headers(Content_Type="text/html"),
            "<html><body>hello world</body></html>")
        flow.reply(resp)
于 2016-01-08T14:36:03.320 回答
0

您还可以捕获响应并编辑/替换它。

使用 BeautifulSoup 也使事情变得更容易。

我的解决方案是创建一个 html 文件replace.html并将其替换为原始响应:

import mitmproxy
from bs4 import BeautifulSoup

def response(flow):
    if flow.request.pretty_host.endswith("example.com/index.php"):
        soup.BeautifulSoup(open("replace.html"))
        flow.response.content = str(soup).encode("utf8")
于 2016-11-08T07:48:48.897 回答