揭秘抓包利器:Python和Mitmproxy让您轻松实现接口请求抓取与分析!

在接口测试和自动化领域,我们经常需要抓取网络请求,分析接口参数和响应,以便更好地理解接口的行为和性能。借助 Mitmproxy 这个强大的抓包工具,我们能够轻松地监控和分析接口请求,为接口自动化测试提供更多的可能性。

什么是 Mitmproxy?

Mitmproxy 是一个开源的中间人代理工具,它能够截取和修改 HTTP 和 HTTPS 流量。它提供了命令行界面和 Web 界面,能够帮助我们实时查看和分析网络请求。

准备工作:安装 Mitmproxy

在开始之前,我们首先需要安装 Mitmproxy。可以通过 pip 命令来安装 Mitmproxy:

pip install mitmproxy

编写 Python 脚本:抓取请求并保存到 CSV

使用 Mitmproxy 来拦截接口请求,并将请求记录保存到 CSV 文件中。

# filename: request_recorder.py
import csv
from mitmproxy import http
class RequestRecorder:
def __init__(self):
self.records = []
def request(self, flow: http.HTTPFlow):
if "api.xxx.com" in flow.request.url:
# 获取请求的 URL、方法、请求头和请求参数
url = flow.request.url
method = flow.request.method
headers = dict(flow.request.headers)
params = dict(flow.request.query or {})
record = {
"URL": url,
"Method": method,
"Headers": headers,
"Params": params,
}
self.records.append(record)
def done(self):
# 保存记录到 CSV 文件
with open("api_requests.csv", "w", newline="", encoding="utf-8") as csvfile:
fieldnames = ["URL", "Method", "Headers", "Params", "Response"]
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
for record in self.records:
writer.writerow(record)
addons = [
RequestRecorder()
]

启动 Mitmproxy 并运行脚本

在终端中运行以下命令启动 Mitmproxy,并加载我们编写的脚本 request_recorder.py

mitmdump -s request_recorder.py

设置浏览器代理

打开浏览器,并将代理地址设置为 http://localhost:8080

访问接口

在浏览器中访问示例接口 https://api.xxx.com/data。Mitmproxy 将拦截这个请求并将其保存到 CSV 文件.

查看保存的数据

在脚本运行完成后,当前目录下会生成一个名为 api_requests.csv 的 CSV 文件:

图片

总结

以上就是勇哥今天为各位小伙伴准备的内容,如果你想了解更多关于Python自动化测试的知识和技巧,欢迎关注我:公众号\博客\CSDN\B站:测试玩家勇哥;我会不定期地分享更多的精彩内容。感谢你的阅读和支持!


题外话,勇哥打算把新建的技术交流群,打造成一个活跃的高质量技术群。工作中遇到的技术问题,都可以在里面咨询大家,还有工作内推的机会。有兴趣的小伙伴,欢迎加我(记得备注是进群还是报名学习)👇👇👇****

图片👆****👆********👆长按上方二维码2秒,关注我


勇哥,10年落魄测试老司机,技术栈偏python,目前在一家超大型房产公司担任自动化测试主管,日常工作比较繁杂,主要负责自动化测试,性能测试、软件质量管理及人员管理。工作之余专注于为粉丝进行简历修改、面试辅导、模拟面试、资料分享、一对一自动化测试教学辅导等副业发展。目前已服务十多位小伙伴,取得高薪offer。

关注公众号,测试干货及时送达图片图片图片

往期精选文章👇:

posted @   测试玩家勇哥  阅读(434)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
💬
评论
📌
收藏
💗
关注
👍
推荐
🚀
回顶
收起
🔑
  1. 1 DAN DAN 心魅かれてく(Cinema Version) FIELD OF VIEW
  2. 2 ロマンティックあげるよ 橋本潮
  3. 3 INNOCENT PRIMEVAL BREAKER GAME VERSION REOL
  4. 4 METAMORPHOSIS INTERWORLD
METAMORPHOSIS - INTERWORLD
00:00 / 00:00
An audio error has occurred, player will skip forward in 2 seconds.

作曲 : IVAN BELOZEROV

I'ma pimp hoes fallin' for this pimpin' rap game

Slowly hit the tempo, just enough to keep these b*****s tame

Makin' fame off a n***a d**k, b*****s suckin' d**k

Make it quick, off my d**k, yeah, I took a foreign chick

Bankroll, other ho just a victim that I chose

Have you suckin' til it's swole, I be bonin' other hoes

N***s wanna learn the game, but keep talkin' shit

I'lI be forced to trash 'em, may the best man win this

No, I'ma pimp hoes fallin' for this pimpin' rap game

Slowly hit the tempo, just enough to keep these b*****s tame

Makin' fame off a n***a ****, b*****s suckin' d**k

Make it quick, off my d**k, yeah, I took a foreign chick

Bankroll, other ho just a victim that I chose

Have you suckin''til it's swole, I be bonin' other hoes

Niggas wanna learn the game, but keep talkin' shit

I'll be forced to trash 'em, may the best man win this

I'ma pimp hoes fallin' for this pimpin' rap game

Slowly hit the tempo, just enough to keep these b*****s tame

Makin' fame off a n***a d**k, b*****s suckin' d**k

Make it quick, off my d**k, yeah, I took a foreign chick

Bankroll, other ho just a victim that I chose

Have you suckin''til it's swole, I be bonin' other hoes

N****s wanna learn the game, but keep talkin' s**t

l'll be forced to trash 'em, may the best man win this

I'ma pimp hoes fallin' for this pimpin' rap game

Slowly hit the tempo, just enough to keep these b*****s tame

Makin' fame off a n***a d**k, b*****s suckin' d**k

Make it quick, off my d**k, yeah, I took a foreign chick

Bankroll, other ho just a victim that I chose

Have you suckin''til it's swole, I be bonin' other hoes

N****s wanna learn the game, but keep talkin' s**t

I'll be forced to trash 'em, may the best man win this