mimproxy一些方法

from mitmproxy import ctx, flowfilter

class Recorder:

def requestheaders(self, flow):
ctx.log.info('requestheaders')

def request(self, flow):
# 日志打印
# ctx.log.info('-----------------------')
# ctx.log.error('-----------------------')

request 常用api

print(flow.request.host) # 当前请求的host

print(flow.request.path) # 请求的接口地址

print(flow.request.pretty_url) # 完整的url路径

print(flow.request.url) # 完整的url路径

print(flow.request.method) # 请求方式

print(flow.request.scheme) # http请求还是https请求

print(flow.request.headers) # 获取所有头信息,包含Host、User-Agent、Content-type等字段

GET请求

获取参数

flow.request.query 请求参数 数据格式 mitmproxy数据类型 类似列表套着元组

flow.request.query.keys() 请求参数key拆分成迭代器,所有keys

flow.request.query.items() 同时循环请求参数的key value

for k,v in flow.request.query.items():

print(k)

print(v)

flow.request.query.values() 请求参数value拆分成迭代器,所有values

flow.request.query.get('name') 通过指定key获取value

print(flow.request.query.get('name'))

flow.request.query.get_all('name') 通过key 获取所有这个key下的value

print(flow.request.query.get_all('name'))

flow.request.query.fields 元素的方式获取所有的请求参数

print(flow.request.query.fields)

修改更新参数

flow.request.query.set_all('sign',['123']) 修改get请求的参数

flow.request.query.update() 修改请求的参数 根据参数名 参数格式要求列表套元组 [('userid',2180),('sign','2beb50f2669b0531d7905e56a6e90bf4')]

增加参数

flow.request.query.add('userid',2179) 增加get请求参数 参数1:key 参数2:value

删除参数

flow.request.query.pop('userid') 删除指定某个请求参数 参数:key

POST请求

获取参数

print(flow.request.get_text()) # 请求中body内容,那么可通过此方法获取,字符串类型

print(flow.request.urlencoded_form) # content-type:application/x-www-form-urlencoded时的请求参数

print(flow.request.multipart_form) # content-type:multipart/form-data 的请求参数

修改更新参数

flow.request.set_text('123') # 设置整体的请求参数 接收字符串

print(dir(flow.request))

pass

def responseheaders(self, flow):
ctx.log.info('responseheaders')

def response(self, flow):
# print(flow.response.get_text()) 获取返回值结果 结果类型是字符串
# print(flow.response.get_content()) 获取返回值结果 结果类型是bytes
# print(flow.response.content) 获取返回值结果 结果类型是bytes二进制
# print(flow.response.status_code) 状态码
# print(flow.response.text) 获取返回值结果 结果类型是字符串
# flow.response.set_text('123') 修改返回值 需要字符串类型

print(dir(flow.response))

pass

def error(self, flow):
print(f'HTTP Error With [{flow.response.reason}], and body: {flow.response.text}')

addons = [
Recorder()

posted @ 2022-10-14 15:46  paomianzhong  阅读(254)  评论(0)    收藏  举报