课后作业
- 使用mitmproxy,贴出来脚本内容和效果截图
- 对第一个股票保持原样
- 对第二个股票名字加长一倍
- 对第三个股票名字变成空
import json
from mitmproxy import ctx, http
class Counter:
def __init__(self):
self.num = 0
def request(self, flow: http.HTTPFlow):
self.num = self.num + 1
ctx.log.info("We've seen %d flows" % self.num)
def response(self, flow: http.HTTPFlow):
# 通过url确定对应的接口信息
if "https://stock.xueqiu.com/v5/stock/batch/quote.json?_t=" in flow.request.pretty_url:
# 修改原始数据
# 获取的text 是str类型,如果要对数据进行操作,需要进行数据转换
data = json.loads(flow.response.text)
data['data']['items'][1]['quote']['name'] = '中国平安'*2
data['data']['items'][2]['quote']['name'] = ' '
# 赋值给响应信息
flow.response.text = json.dumps(data)
addons = [
Counter()
]
import json
from mitmproxy import ctx, http
class Counter:
def __init__(self):
self.num = 0
def request(self, flow: http.HTTPFlow):
self.num = self.num + 1
ctx.log.info("We've seen %d flows" % self.num)
def response(self, flow: http.HTTPFlow):
# 通过url确定对应的接口信息
if "https://stock.xueqiu.com/v5/stock/batch/quote.json?_t=" in flow.request.pretty_url:
# 加载本地的json文件
with open('2.json', encoding='utf-8') as f:
data = json.load(f)
# 赋值给响应信息
flow.response.text = json.dumps(data)
addons = [
Counter()
]
class Counter:
def __init__(self):
self.num = 0
def request(self, flow: http.HTTPFlow):
self.num = self.num + 1
ctx.log.info("We've seen %d flows" % self.num)
# 判断是否是想要的请求信息,通过url进行判断
def response(self, flow: http.HTTPFlow):
name_url = "https://stock.xueqiu.com/v5/stock/batch/quote.json?_t="
if name_url in flow.request.pretty_url:
print("---------begin---------")
# 修改原始数据
data = json.loads(flow.response.text)
data["data"]["items"][1]["quote"]["name"] = "阿里巴巴" * 2
data["data"]["items"][2]["quote"]["name"] = ""
# 赋值给响应信息
flow.response.text = json.dumps(data)
else:
print("no exists!!!!!")
addons = [
Counter()
]
map_local 实现:
rewrite实现:
算法实现:
rewrite
import json
import sys
from mitmproxy import http
from mitmproxy import ctx
from mitmproxy.tools.main import mitmdumpclass Counter:
def init(self):
self.num = 0def request(self, flow: http.HTTPFlow): self.num = self.num + 1 ctx.log.info("We've seen %d flows" % self.num) # 判断是否是想要的请求信息,通过url进行判断 def response(self, flow): if "https://stock.xueqiu.com/v5/stock/batch/quote.json?_t=" \ in flow.request.pretty_url: # 修改原始数据 # 获取的text 是str类型,如果要对数据进行操作,需要进行数据转换 data = json.loads(flow.response.text) old_data = data["data"]["items"][1]["quote"]["name"] data["data"]["items"][1]["quote"]["name"] = old_data * 2 data["data"]["items"][2]["quote"]["name"] = "" flow.response.text = json.dumps(data) # 赋值给响应信息
addons = [
Counter()
]if name == ‘main’:
sys.argv = [file, “-s”, file]
#
# 官方要求必须主线程
mitmdump()
map_local
import json
import sys
from mitmproxy.tools.main import mitmdumpclass Counter:
def init(self):
self.num = 0def response(self, flow): if "https://stock.xueqiu.com/v5/stock/batch/quote.json?_t=" \ in flow.request.pretty_url: # 打开文件,读取文件数据,作为响应,给返回 with open("new_data.json", encoding="UTF-8") as f: data = json.load(f) flow.response.text = json.dumps(data)
addons = [
Counter()
]if name == ‘main’:
sys.argv = [__file__, "-s", __file__] # # 官方要求必须主线程 mitmdump()
算法
import json
import sys
from mitmproxy import http
from mitmproxy.tools.main import mitmdumpclass Counter:
def init(self):
self.num = 0
# 定义股票编码
self.first_symbol = “RLX”
self.second_symbol = “SZ000100”def response(self, flow: http.HTTPFlow): # 便于拿到含股票名称的接口响应 if "/v5/stock/batch/quote.json?_t" \ in flow.request.pretty_url \ and ((self.first_symbol in flow.request.pretty_url) \ or (self.second_symbol in flow.request.pretty_url)): # 打开文件,读取文件数据,作为响应,给返回 data = json.loads(flow.response.text) self.json_travel(data) flow.response.text = json.dumps(self.json_travel(data)) def json_travel(self, data): # 判断传入的数据类型{"a": {"b":{"c":1}}} if isinstance(data, dict): for key, value in data.items(): # 响应结果替换,将第二支股票名称加倍,第三支股票名称去掉 if key == "items": old_data = data["items"][1]["quote"]["name"] new_data = old_data * 2 data["items"][1]["quote"]["name"] = new_data data["items"][2]["quote"]["name"] = "" else: # 遍历字典的数据 # 当字典格式,递归value值 data[key] = self.json_travel(value) elif isinstance(data, list): # 当数据类型 为 list 的时候, 添加到结构内,并继续递归遍历, # 知道数据类型不为可迭代对象时 data = [self.json_travel(value) for value in data] elif isinstance(data, bool): data = data elif isinstance(data, int) or isinstance(data, float): data = data elif isinstance(data, str): data = data else: data = data return data
addons = [
Counter()
]if name == ‘main’:
sys.argv = [file, “-s”, file]
mitmdump()
修改前
修改后
class Counter(object):
“”"
request, response 皆为固定写法,对应每一次请求与响应
“”"
def init(self):
self.num = 0
def request(self, flow: http.HTTPFlow):
self.num = self.num + 1
ctx.log.info("We've seen %d flows" % self.num)
# 判断是否是想要的请求信息,通过url进行判断
def response(self, flow):
# 抓包确定需要修改的信息在哪个接口中,并判断是否在pretty_url中
if "https://stock.xueqiu.com/v5/stock/batch/quote.json?_t=" in flow.request.pretty_url:
# 修改原始数据
# 获取的text 是str类型,如果要对数据进行操作,需要进行数据转换
data = json.loads(flow.response.text)
# 依次修改123只股票的名称
data["data"]["items"][0]["quote"]["name"] = data["data"]["items"][0]["quote"]["name"]
data["data"]["items"][1]["quote"]["name"] = data["data"]["items"][1]["quote"]["name"]*2
data["data"]["items"][2]["quote"]["name"] = ""
# 赋值给响应信息
flow.response.text = json.dumps(data)
addons = [
Counter()
]
作业请查收:
修改前
修改后