charles+flask实现动态mock
直入正题
工作遇到需要测试投放的广告效果,广告效果图是这样的
想把广告图换成自己公司的图片上只要通过charles改改改就完了,但是怎么动态的改改改呢?
1.charles抓包设置
2.设置map remote
找到关键请求
打开Map Remote
map remote配置
开启map remote
实现思路
看到这很多同学可能就明白了,这不就是以前网络安全课或者在书上看到的黑客常用的攻击手段吗,没错,咱们今天体验一把
3.书写中转器
- 安装flask
1
pip install flask
1 | #-*-coding:UTF-8-*- |
替换方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23def replace_txt(json_data):
data_id = ""
for dic in json_data["data"]:
data_id = dic
list = json_data.get("data").get(data_id).get('list')
for ad in list:
print(ad)
ad['txt'] = "网易新闻"
ad['desc'] = "人体75%都是水,喝够水的人生到底有多赚?"
ad['img']="http://cms-bucket.nosdn.127.net/fa04e6870fd647c8b49cdf4719247a6b20180531114309.jpeg?imageView&enlarge=1&thumbnail=1200y800&quality=85"
ad['img2']="http://pgdt.gtimg.cn/gdt/0/9c59c1ca41aea62d77328526c4ef5eb9.PNG/0"
ad['customized_invoke_url']="newsapp://doc/DJ4OSETE0511A0AV?spsAdDocId=DJ4OSETE0511A0AV&spsPrice=0&spsPicId=http%3A%2F%2Fcms-bucket.nosdn.127.net%2Ffa04e6870fd647c8b49cdf4719247a6b20180531114309.jpeg%3FimageView%26enlarge%3D1%26thumbnail%3D1200y800%26quality%3D85&spsAdPlanId=176&spsModelId=0&s=news_zhizi_android_01&spsAdId=2&spsExpandId=291&spsImpressionId=441203903548&spsextend=test2"
if not ad.get('ext'):
ad['ext'] = {}
ad['ext']['appname']="网易新闻"
ad['ext']['pkg_name']="com.netease.newsreader.activity"
ad['ext']['appid']=100812722
ad['pkg_download_schema']="taskApkId=0&packName=com.netease.newsreader.activity&taskAppId=&appId=&via=ANDROIDQQ.FEED.ADVERTISE.zvqj62cnik7wo01&versionCode=652&channel=000116083636353430393231;70225816"
# ad['rl']="newsapp://doc/DJ4OSETE0511A0AV?spsAdDocId=DJ4OSETE0511A0AV&spsPrice=0&spsPicId=http%3A%2F%2Fcms-bucket.nosdn.127.net%2Ffa04e6870fd647c8b49cdf4719247a6b20180531114309.jpeg%3FimageView%26enlarge%3D1%26thumbnail%3D1200y800%26quality%3D85&spsAdPlanId=176&spsModelId=0&s=news_zhizi_android_01&spsAdId=2&spsExpandId=291&spsImpressionId=441203903548&spsextend=test2"
# print(list)
json_data['data'][data_id]["list"]=list
return json_data请求接收-处理-返回(处理部分:替换请求url,将返回的response替换成我们需要的response)
1
2
3
4
5
6
7
8
9@server.route('/information',methods=["get","post"]) # server.route装饰器装饰方法
def information():
url = flask.request.url
# print("url:",url)
url = url.replace("http://47.94.108.11:8999","http://info.gdt.qq.com")
res = requests.get(url).json()
if 'push' not in url:
res = replace_txt(res)
return json.dumps(res,ensure_ascii=False)启动server
1
server.run(host='0.0.0.0',port=8999,debug=True) # server的启动