mitmproxy处理请求及乱码

当有了请求之后,怎么操作我们的返回呢?同样的创建方法,同样需要有一个flow,可以理解为承上启下的对象,一个上下文句柄,这样我现在已经拥有了flow,那么久可以通过flow.response获取我们所有的返回数据,获取到了我们就可以把数据打印出来,例如状态码,response_data.status_code ,还有我们的响应结果,response_data.text,这时就拥有了我们所有的数据,可以打印出来

def response(flow):
    response_data = flow.response
    print('code------------>',response_data.status_code)
    print('res_data-------------------->',response_data.text)

在这里插入图片描述
发现很多乱码的数据,我们先不管乱码不乱码,至少看到了这些数据,而且通过浏览器数据是拥有的,只是图片而已
在这里插入图片描述
那么怎么处理乱码,打开浏览器,查看请求,因为我们返回的是图片就是说现在的content-type是image,那能不能通过这个content-type进行判断,如果不是我想要的,我就给你过滤掉,所以这时候就要从header下手,
在这里插入图片描述


def response(flow):
    response_data = flow.response
    response_header = response_data.headers
    content_type = response_header['Content-Type']
    print('content_type---------------->',content_type)
    print('code------------>',response_data.status_code)
    print('res_data-------------------->',response_data.text)

在这里插入图片描述
这里发现,图片格式—乱码

所以我们要判断,如果content-type中带有image就是个图片,咱们就不打印结果,else打印结果,来更新代码,实现一下

def response(flow):
    response_data = flow.response
    response_header = response_data.headers
    content_type = response_header['Content-Type']
    if 'image' in content_type:
        print('这里返回的是图片')
    else:
        print('content_type---------------->',content_type)
        print('code------------>',response_data.status_code)
        print('res_data-------------------->',response_data.text)

在这里插入图片描述
这里我们的响应值看起来就干净了很多,现在就完成了一个结合,既可以拿到你的请求数据,也可以拿到你的返回数据,也可以说我监听了整个app操作过程中所有数据的走向,我想怎么去操作,就可以怎么去操作,相对而言就轻松了很多,也可以这样编码,在格式为appliction/json的情况下打印,否则格式不正确

def response(flow):
    response_data = flow.response
    response_header = response_data.headers
    content_type = response_header['Content-Type']
    if 'image' in content_type:
        print('这里返回的是图片')
    elif 'application/json' in content_type:
        print('content_type---------------->', content_type)
        print('code------------>', response_data.status_code)
        print('res_data-------------------->', response_data.text)
    else:
        print('格式非预期')
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值