mitmproxy拦截接口

import mitmproxy.http
from mitmproxy import ctx
from mitmproxy import flowfilter
import json

class Interceptor:

    def __init__(self):
        # 添加网址过滤器
        self.filter = flowfilter.parse("~u https://httpbin.org")

    def request(self, flow: mitmproxy.http.HTTPFlow):
        if flowfilter.match(self.filter, flow):  
            ctx.log.info("match request")
            # 替换搜索词
            flow.request.query["wd"] = "富婆通讯录"

    def response(self, flow: mitmproxy.http.HTTPFlow):
        if flowfilter.match(self.filter, flow):
            ctx.log.info("match response")
            # 添加/修改headers
            flow.response.headers["md5"] = "00112233445566778899AABBCCDDEEFF"
            # flow.response.content = bytes("想啥呢",encoding='utf8')
            data = flow.response.text
            js = json.loads(data)
            js['data'] = 'test'
            flow.response.text = json.dumps(js)


    #
    # def request(flow: http.HTTPFlow) -> None:
    #     if flow.request.method == "CONNECT":
    #         # If the decision is done by domain, one could also modify the server address here.
    #         # We do it after CONNECT here to have the request data available as well.
    #         return
    #         client_ip = flow.client_conn.address[0]
    #         if 'ip.cn' in flow.request.url:
    #             ctx.log.info(flow.request.url)
    #             proxy = ("localhost", 8888)
    #         else:
    #             proxy = ("localhost", 3800)
    #     # 这里配置二级代理的ip地址和端口
    #     if flow.live:
    #         flow.live.change_upstream_proxy_server(proxy)

addons = [
    Interceptor()
]
  • 5
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
前端可以通过以下方法拦截接口报错: 1. 使用try-catch语句:在发起接口请求的地方使用try-catch语句来捕获接口请求时可能抛出的异常。在catch块中可以处理异常情况,例如展示错误提示信息或者进行其他操作。 ```javascript try { // 发起接口请求 const response = await axios.get('/api/data'); // 处理返回的数据 } catch (error) { // 处理接口请求异常 console.error('接口请求异常:', error); // 展示错误提示信息或者进行其他操作 } ``` 2. 使用axios拦截器:如果你使用的是axios库发送接口请求,可以使用axios的拦截器来统一处理接口错误。通过在响应拦截器中捕获错误并进行处理。 ```javascript axios.interceptors.response.use( (response) => { // 处理返回的数据 return response; }, (error) => { // 处理接口请求异常 console.error('接口请求异常:', error); // 展示错误提示信息或者进行其他操作 return Promise.reject(error); } ); ``` 3. 使用全局错误处理:在Vue.js等前端框架中,可以通过捕获全局错误来处理接口报错。在Vue的全局错误处理函数中,捕获到错误后可以进行相应的处理。 ```javascript Vue.config.errorHandler = function (err, vm, info) { // 处理接口请求异常 console.error('接口请求异常:', err); // 展示错误提示信息或者进行其他操作 } ``` 以上是一些常见的拦截接口报错的方法,你可以根据自己的项目需求选择合适的方式来处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值