fiddler小技巧-TXT转CSV
Posted 飞测
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了fiddler小技巧-TXT转CSV相关的知识,希望对你有一定的参考价值。
坚持的是分享,搬运的是知识,图的是大家的进步,没有收费的培训,没有虚度的吹水,喜欢就关注、转发(免费帮助更多伙伴)等来交流,想了解的知识请留言,给你带来更多价值,是我们期待的方向,有更多兴趣的欢迎切磋,我们微信订阅号,联系方式如下:
更多书籍,敬请期待
TXT 保存接口文档内容 利用fiddler工具保存到txt 怎么操作可看fiddler小技巧
接口传入参数和响应数据 :
Request url:
Request headers: --> 请求头
{'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8','Authorization': 'DigestSystemType="2",uType="1",bDChannelId="",bDUserId="",AppBuild="237",uUID="55485"'}
Request body: GroupId=572&AtUserIds=12821|2 --->传入参数Response code: 200Response body:{"StatsCode":200,"Message":"操作成功","Data":null,"Other":null} -->响应数据Session end
使用python 获取文档内容到list:
def __init__(self):
self.sessions_path ="D:\Fiddler\Api"
self.net = 0
self.json_list = []
def get_all_session(self):
"""
获取所有请求
:return:
"""
files = self.__get_file_list()
l2 = []
for i2 in files:
l2.append(self.__get_single_session(i2))
self.net = self.net +1
return l2
def __get_file_list(self):
"""
"""
for root, dirs, files in os.walk(self.sessions_path):
return (f for f in files)
def __get_single_session(self,path):
"""
获取单个文件的所有请求(单个请求的url,请求参数,响应体)
:return:
"""
single_session = []
total_session = []
try:
l1 = open('%s%s%s' % (self.sessions_path,"\\", path), 'r',encoding='utf-8').readlines()
except UnicodeDecodeError:
l1 = open('%s%s%s' % (self.sessions_path,"\\", path), 'r',encoding='utf-16-le').readlines()
for i1 in l1:
if not i1.startswith("\n"):
if i1.startswith("Request url: "):
single_session.append(i1.split("Request url:")[-1].replace("\n", ""))
if i1.startswith("Request body: "):
single_session.append(i1.split("Requestbody: ")[-1].replace("\n", ""))
if i1.startswith("Response body:"):
single_session.append(i1.split("Response body:")[-1].replace("\n", ""))
if i1.startswith("Session end"):
if len(single_session) != 0:
total_session.append(single_session)
single_session = []
return total_session
o 比如这就是获取的数据
[['', 'CircleId=101315&IsAdd=1', '{"StatsCode":200,"Message":"收藏成功","Data":null,"Other":null}'],['', 'CircleId=101315&IsAdd=1','{"StatsCode":200,"Message":"收藏成功","Data":null,"Other":null}']]
获取的数据存到CSV表格里面
def request_data(self):
"""
获取request_data文本信息存在列表 l 里面
:return:
"""
s = utils.ReadSessions.ReadSessions()
sessions = s.get_all_session()
l = []
for i in sessions:
for j in i:
l.append(j)
return l
o 然后创建CSV表格且存进去
def test_csv(self,l):
"""
:param l: 保存到CSV文件夹
:return: CSV无法设置高宽数据
"""
outputFile = open(os.path.join(os.getcwd(), "test2.csv"), 'w',newline='',
encoding='GB18030') # gbk存入会报错 utf-8传入会乱码
outputWriter = csv.writer(outputFile)
case1 = 1001
testcase = 1
print("准备导入数据")
outputWriter.writerow(
['MyorDer', 'DomainName', 'MymeThod','MyName', 'Myurl', 'ExpEcted', 'TestResults', 'TestCase'])
for w in l:
if w[0].startswith("http://"):
h = w[0].split("/api/")[0]
data = {"info": [{"t_id":case1,
"Dom_Name":h.split("p://")[-1],
"t_method": "post",
"t_url":w[0].split("api/")[-1],
"t_param": w[1],
"t_actual": w[2],
"t_result": "测试"}]}
for item in data["info"]:
print("导入数据中 : %s" %item["Dom_Name"] + item["t_url"] + "...")
try:
outputWriter.writerow([item["t_id"],item["Dom_Name"], item["t_method"],item["t_url"],
item["t_param"], item["t_actual"],item["t_result"]])
except UnicodeEncodeError:
print(item["t_actual"])
case1 = case1 + 1
testcase = testcase + 1
print("数据导入完毕")
outputFile.close()
main代码
if __name__ =="__main__":
r = CSVtest()
StartTime = time.time()
l = r.request_data()
ZanTime = time.time()
r.test_csv(l)
EndTime = time.time()
print("请求时间 : %s",(ZanTime - StartTime))
print("写入时间 : %s",(EndTime - StartTime))
3后语
推荐的文章
↓↓↓ 点击"阅读原文" 【查看更多信息】
以上是关于fiddler小技巧-TXT转CSV的主要内容,如果未能解决你的问题,请参考以下文章