pytho 解析fiddler 导出的har文件代码,自动录入api
参考:https://testerhome.com/topics/5276 har导出格式如下: {
代码如下: #!/usr/bin/env python
# -*- coding: utf-8 -*-
from optparse import OptionParser
import os
import sys
import json # check env
if sys.version_info < (3, 4):
raise RuntimeError('At least Python 3.4 is required.') restype = ('js', 'css', 'jpg', 'gif', 'ico', 'png') def list2dic(headers):
header_dic = dict()
for head in headers:
if head['name'] in header_dic:
header_dic[head['name']] = header_dic[head['name']] + ',' + head['value']
else:
header_dic[head['name']] = head['value']
return header_dic def dictoand(dct):
res_list = list()
for tp in dct:
res_list.append('%s=%s' % (tp['name'], tp['value']))
return '&'.join(res_list) def dict2lr(lrsc):
tmpl = '''
web_custom_request("%(name)s",
"URL=%(url)s",
"Method=%(method)s",
"Resource=%(res)s",
"Referer=%(referer)s",
"EncType=%(enctype)s",
"Body=%(body)s",
LAST);'''
# url
url = lrsc['url']
method = lrsc['method']
name = url.split('/')[-1]
name = name.split('?')[0]
suff = url.split('.')[-1]
# Resource type
global restype
res = '0'
if suff in restype:
res = '1' # Content-Type
enctype = ''
if 'Content-Type' in lrsc:
enctype = lrsc['Content-Type']
# Referer
referer = ''
if 'Referer' in lrsc:
referer = lrsc['Referer'] # Body
body = ''
if 'posttext' in lrsc:
body = lrsc['posttext']
elif 'postparams' in lrsc:
body = dictoand(lrsc['postparams'])
body = body.replace('"', '\\"')
res = tmpl % {'name': name, 'url': url, 'method': method, 'enctype': enctype, 'referer': referer, 'res': res,
'body': body}
# Head
if 'SOAPAction' in lrsc:
res = ("\n" + ' web_add_header("SOAPAction", "%s")' + ";\n" + res) % lrsc['SOAPAction']
return res def parhar(harfile):
print('harfile:',harfile)
res = list()
try:
import codecs
# with codecs.open(harfile, 'r', 'utf-8-sig') as in_file:
# for line in in_file.readlines():
# print(line)
FH = codecs.open(harfile, mode='r', encoding='utf-8-sig')
# FH = open(harfile, mode='r')
print('FH:',FH)
all = json.load(FH)
FH.close()
except Exception as ex:
print('Open har file errr: %s' % ex)
quit() print(all)
har_ver = all['log']['version']
creater = all['log']['creator']['name']
entries = all['log']['entries']
ct = len(entries) # 请求实体数据
for et in entries:
stm = et['startedDateTime']
req = et['request'] # 请求数据
rsp = et['response'] # 返回数据
lrsc = dict()
if '_charlesStatus' in rsp and rsp['_charlesStatus'] != 'Complete':
continue
# 获取请求入参的主要数据
lrsc['method'] = req['method']
lrsc['url'] = req['url']
headers = req['headers']
print('headers:', headers)
# http head
header_dic = list2dic(headers)
print('headers dict:', header_dic)
if 'SOAPAction' in header_dic:
lrsc['SOAPAction'] = header_dic['SOAPAction'].replace('"', '\\"')
if 'Referer' in header_dic:
lrsc['Referer'] = header_dic['Referer']
if 'Content-Type' in header_dic:
lrsc['Content-Type'] = header_dic['Content-Type']
if lrsc['method'] == 'GET':
pass
elif lrsc['method'] == 'POST':
if 'postData' in req:
if 'text' in req['postData']:
lrsc['posttext'] = req['postData']['text']
if 'params' in req['postData']:
lrsc['postparams'] = req['postData']['params']
if 'mimeType' in req['postData']:
lrsc['postmime'] = req['postData']['mimeType']
else:
continue
print('V:',lrsc)
res.append(dict2lr(lrsc))
return res if __name__ == '__main__':
parse = OptionParser()
parse.add_option("-f", action="store", dest="harfile", help='harfile path')
parse.add_option("-o", action="store", dest="lrfile", help='action.c path')
(options, args) = parse.parse_args()
print('-----------------------')
print(options)
print('-----------------------')
print(args) if options.harfile is None or options.lrfile is None:
parse.print_help()
quit()
if not os.path.exists(options.harfile):
print('Har file %s not exist' % options.harfile)
quit()
print("1")
res = parhar(options.harfile)
print("**********************************8888888")
print(res)
file = open(options.lrfile, mode='w', encoding='utf-8')
for sc in res:
file.write(sc)
file.write("\n")
file.close()
print('Output to %s' % options.lrfile)
pytho 解析fiddler 导出的har文件代码,自动录入api的更多相关文章
- eclipse 设置打开java文件代码自动折叠
eclipse 设置打开java文件代码自动折叠 java: windows/preference/java/editor/folding->enable folding 可以在里面设置所要折叠 ...
- java poi导出EXCEL xls文件代码
String _currentPage = request.getParameter("currentPage"); Integer currentPage = 0; if(_cu ...
- 设置Eclipse的类文件和xml文件代码自动补全
原文:https://blog.csdn.net/erlian1992/article/details/53706736 我们在平常编写代码的时候,不会记住大多数的类和文件的属性,方法等等,这就需要我 ...
- thinkphp5自动生成文档/注释代码自动生成api文档
composer require weiwei/api-doc dev-master 安装之后,readme 有详细的使用说明代码: 部分界面: gitbub:https://github.com/z ...
- Fiddler抓包一键生成代码
首先我们的需求场景是 用Fiddler抓到某个接口调用后,用代码来模拟调用,一般我们写代码会有3个步骤: 1设置http请求相关的参数:header,method,url,cookie等 2设置pos ...
- Chrome保存的HAR文件怎么打开?
- Chrome保存HAR 在Chrome中,在需要抓包的任意一个浏览器窗口,按F12,点Network页面,即可进入抓包界面,之后的所有网页交互操作产生的报文,都会在此列出. 在抓包的报文界面上右键 ...
- Python 解析har 文件将域名分类导出
前言 作为程序员平时主要是使用 shadowsocks 作为代理工具的.shadowsocks 有个很明显的优点儿就是可以设置白名单和黑名单.白名单是会走shadowsocks的自动代理模式. 遇到的 ...
- html文件在head标签中引入js地址和直接写js代码,所用时间是不同的,因为引入js地址,文件加载的时候需要通过通讯协议去解析地址,读取外部文件
html文件在head标签中引入js地址和直接写js代码,所用时间是不同的,因为引入js地址,文件加载的时候需要通过通讯协议去解析地址,读取外部文件
- Twaver的mono-desiner导出的json文件解析
以画的交换机为例,其他大概都差不多. 利用Twaver做出交换机模型如图1所示,其中,每一个端口都是一个单独的对象.具体Twaver操作流程参见网址:http://twaver.servasoft.c ...
随机推荐
- 浅谈html5在vr中的应用
使用过HTML5制作动画过程的开发者都知道,HTML5页面给人一种逼真的感觉,同时HTML也是可以制作VR页面,但是需要你熟练HTML5与JavaScript开发过程,所以在有必要的情况下,我们可以用 ...
- eclipse sts 常规操作
项目:右键 refresh 右键 maven -> update project 重新remove add project 重启软件,电脑 1.项目冗余 Package Explorer ...
- [转帖]JVM内存结构 VS Java内存模型 VS Java对象模型
JVM内存结构 VS Java内存模型 VS Java对象模型 https://www.hollischuang.com/archives/2509 Java作为一种面向对象的,跨平台语言,其对象.内 ...
- 【转】mysqldump原理探究
作者:胡儿胡儿 来源:CSDN 原文:https://blog.csdn.net/cug_jiang126com/article/details/49824471 —————————————————— ...
- (3.4)常用知识-char与varchar的选择
1.char与varchar的比较 (1)数据存储开销 [1]varchar列需要2个额外的字节来记录存储数据的长度 [2]每个可为null的char列,需要一些字节(空位图)来反应数据的为空性 [3 ...
- Windows 下安装 ElasticSearch 修改 elasticsearch.yml的坑
注意: ElasticSerach 集成 IK分词器 的时候,整个路径不能有空格!!! 1. 文件后加入 严格复制粘贴,否则入坑 http.cors.enabled : true http.cors ...
- MySQL-快速入门(7)索引
1.什么是索引 索引是对数据库表中一列或者多列的值进行排序的一种结构.索引是在存储引擎中实现的,每种存储引擎中的索引不一定完全相同. MySQL中索引的存储类型有两种:btree和hash.MyISA ...
- net 架构师-数据库-sql server-001-SQL Server中的对象
1.1 数据库的构成 1.2 数据库对象概述 1.2.1 数据库对象 RDBMS 关系数据库管理系统 对象:数据库.索引.事务日志.CLR程序集.表 .报表.文件组.全文目录.图表.用户自定义数据类型 ...
- oracle管理基础知识
1.oracle的安装 win下 linux下 2.内存和后台进程=实例 为何将oracle做的如此复杂呢 1.内存: --提高查询速度 --提升处理数据的速度 2.后台进程 --为了完成特定的服务, ...
- ELK + filebeat集群部署
ELK + filebeat集群部署 一.ELK简介 1. Elasticsearch Elasticsearch是一个实时的分布式搜索分析引擎, 它能让你以一个之前从未有过的速度和规模,去探索你的数 ...