首页 > 解决方案 > 拦截标头并即时修改

问题描述

我的任务是开发一个 mitmproxy 脚本,这是我以前从未做过的事情。

我正在尝试在 mitmproxy 中的反向代理实例上的 http-flow 期间动态修改“位置”标头,这些标头通常不同,但我现在的主要目标是简单地以静态方式检测它们。

我正在运行 mitmproxy: mitmdump --set flowdetail:3 -s rproxy.py -p 8080 --mode reverse:http://www.com ^ 当然,这不是问题的真实 URL。

假设我有从服务器位置发送的标头:http: //www.com/example-location

但我想将其更改为 Location: http://localhost:8080/example-location

我已经尝试了可以​​在 google 上找到的所有示例,包括 StackOverFlow 上的示例。有些事情我尝试过但没有成功。

from mitmproxy import http
import sys

def response(flow: http.HTTPFlow) -> None:
    flow.request.host = "http://localhost:8080/example-location"

from mitmproxy import http
import sys

def response(flow):
    flow.repsonse.headers['Location'] = ["http://localhost:8080/example-location"]
    flow.repsonse.headers['Location'] = "http://localhost:8080/example-location" ## I tried this line separately without the above line

from mitmproxy import http
import sys

def response(flow: http.HTTPFlow) -> None:
    flow.repsonse.headers['Location'] = ["http://localhost:8080/example-location"]
    flow.repsonse.headers['Location'] = "http://localhost:8080/example-location" ## I tried this line separately without the above line

我该如何正确地做到这一点?

如果无法操纵此标头(但为什么不应该),我有什么选择?

标签: pythonmitmproxy

解决方案


推荐阅读