您现在的位置是:网站首页>文章详情文章详情
mitmproxy直接返回自定义的响应内容
inlike2022-03-02【 原创文章 】 浏览(2870) 评论(0) 喜欢(25)
简介使用mitmproxy自定义响应,报错module 'mitmproxy.http' has no attribute 'HTTPResponse'。
mitmproxy在拦截请求的时候,直接返回自定义的响应内容,老版本使用方法如下:
from mitmproxy import http
def request(flow: http.HTTPFlow):
# redirect to different host
flow.response = http.HTTPResponse.make(300, "OK")
直接这样使用,会导致下列错误:
Addon error: Traceback (most recent call last):
File ".\hook.py", line 19, in request
flow.response = http.HTTPResponse.make(
AttributeError: module 'mitmproxy.http' has no attribute 'HTTPResponse'
直接在请求拦截中,返回响应的正确方法如下:
from mitmproxy import http
def request(flow: http.HTTPFlow):
# redirect to different host
flow.response = http.Response.make(
418, b"I'm a teapot",
)
使用的是http模块下面的Response对象,在新版本中不存在HTTPResponse。使用下面的代码来测试代理的拦截效果:
import requests
proxies = {
'http': 'http://localhost:8080',
'https': 'http://localhost:8080',
}
a = requests.get('http://example.org', proxies=proxies)
效果如下:
标签
相关文章
本栏推荐
标签云
猜你喜欢
站点信息
- 建站时间:2019-5-24
- 网站程序:like in love
- 主题模板:《今夕何夕》
- 文章统计:104条
- 文章评论:***条
- 微信公众号:扫描二维码,关注我们