前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >使用Mitmproxy抓包形成接口文档的实践

使用Mitmproxy抓包形成接口文档的实践

原创
作者头像
中年妇女
发布2022-04-27 17:37:25
5620
发布2022-04-27 17:37:25
举报
文章被收录于专栏:测试技术

1、Mitmproxy安装与配置

1)下载与安装 pip install Mitmproxy

2)配置环境变量 (mitmdump.exe所在的目录)

3) 执行 Mitmdump –version 成功即可

2、编写与mitmdump.exe搭配使用的py脚本

使用前,先配置好代理

抓包mitmdump.exe -p 8999 -s A.py

所有的请求流都会经过A.py进行处理。故可以做的事情比较多,可以处理响应,报文,也可以修改响应等

A.py

from mitmproxy import http

代码语言:javascript
复制
def response(flow):

    # 获取请求对象

    res = flow.response #响应报文
代码语言:javascript
复制
# flow.request 请求报文
代码语言:javascript
复制
   if (flow.request.url.find('.js') != -1):

          r_method = str(flow.request.method)  #请求method

          r_url = str(flow.request.url) #请求url

          r_path = str(flow.request.path) #请求path

          r_http_version = str(flow.request.http_version)# 请求http_version

          r_headers = str(flow.request.headers)#请求header

          tmp =r_headers[7:]

          r_headers_list = eval(tmp)

          r_body = flow.request.text #请求报文 



          p_http_version = str(res.http_version)# 响应httpversion

          p_status_code  = str(res.status_code)#响应code

          p_headers = str(res.headers)#响应header

          tmp = p_headers[7:]

          p_headers_list =eval(tmp)

          p_body = res.text #响应报文

3、python+docx处理

库:from docx import Document

document = Document(file) file可为空

table = document.add_table(rows=1, cols=colss, style='Table Grid')

p = document.add_paragraph(style=style) #添加段落

run = p.add_run(result)

document.add_heading(text=result,level=1) #添加标题

4、需求:自动抓包生成接口文档

1) 类型1:页面包括.htm 与.do类型

.htm为页面,.do为后台发送接口

2) 类型2:vue格式,windows url与抓包显示的不一样。

windows url: host:/agent/#bbbbbb

抓包到的请求为:host:/agent/api/cccc

处理思路为与webdriver 结合,在指定的浏览器下操作

Webdriver打开指定浏览器设置方法如下:

1) 在cmd上运行:

chrome.exe --remote-debugging-port=9222 --user-data-dir="C:\selenum\AutomationProfile"

可以直接打开浏览器

2)在指定的浏览器执行,调用webdriver就可以获取访问的title和url。

然后想办法将title与后续抓取到的接口对应上。

3)webdiver与mitmdump 调用py,属于两个线程,如果放在flow后进行webdriver的操作,会阻塞。故采用多线程处理

Webdriver 获取title:

代码语言:javascript
复制
def GetUrl():
    print(f'这是线程GetUrl')
    while True:

        chrome_options = Options()
        chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:9222")
        # chrome_driver = "C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe"  # 如果将chrome驱动放到Python目录,这句可以不要
        driver = webdriver.Chrome(chrome_options=chrome_options)
        current_url = driver.current_url
        current_title = driver.title
        print("title" + current_title, current_url)

        if (current_url[:4] =='http' and current_url.find('.htm') == -1 ):

            result = json.dumps({'url':current_url,'title':current_title})
        else:
            result = ''

        #先清空,后放
        with open('url.txt' ,'w',encoding='utf-8') as fw:
            fw.write(result)

抓包与webdriver获取title多线程执行为:

代码语言:javascript
复制
def run():
    os.system('mitmdump.exe -p 8999 -s Mit_PackV2.py')

def main():
    thread = threading.Thread(target=GetUrl)
    thread.start()
    # thread1 = threading.Thread(target=test.Run2())
    # thread1.start()
    # thread.join()
    # thread1.join()
    thread1 = threading.Thread(target=run)
    thread1.start()
    thread.join()
    thread1.join()

处理流程如下:

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1、Mitmproxy安装与配置
  • 2、编写与mitmdump.exe搭配使用的py脚本
  • 3、python+docx处理
  • 4、需求:自动抓包生成接口文档
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档