最近看到这个,感觉很好,转载学习一下,支持原作者tzyyy1
fiddler
原地址https://blog.csdn.net/tzyyy1/article/details/98512856
代码
#!/usr/bin/python
# -*- coding: UTF-8 -*-
str_filename = "2.txt"
# save_name = "re_test.py"
save_name = str_filename.replace("txt", "py")
class FidToPy():
def __init__(self, str_name, sa_name):
self.str_filename = str_name
self.save_name = sa_name
self.text = ""
self.url_list = []
self.headers = {}
self.cookies = {}
self.data = {}
def get_url(self):
infos = self.text.split("\n")[0]
self.url_list = [infos.split(" ")[0], infos.split(" ")[1]]
def get_headers(self):
infos = self.text.split("\n")[1:]
info = ""
for i in infos:
if "Cookie: " in i:
break
info += i + "\n"
headers = info.split("\n")
while "" in headers:
headers.remove("")
for i in headers:
if ": " not in i:
break
self.headers[i.split(": ")[0]] = i.split(": ")[1]
def get_cookies(self):
infos = self.text.split("\n")[1:]
cookies_flag = 0
for i in infos:
if "Cookie: " in i:
self.cookies = i.replace("Cookie: ", "")
print(self.cookies )
cookies_flag = 1
break
if cookies_flag==1:
self.cookies = {i.split("=")[0]: i.split("=")[1] for i in self.cookies.split("; ")}
def get_data(self):
try:
infos = self.text.split("\n")
for i in range(2, len(infos)):
if infos[i - 1] == "" and "HTTP" in infos[i + 1]:
self.data = infos[i]
break
self.data = {i.split("=")[0]: i.split("=")[1] for i in self.data.split("&")}
except:
pass
def get_req(self):
info_beg = "#!/usr/bin/python\n# -*- coding: UTF-8 -*-\nimport requests\n\n"
info_url = "url = \'{}\'\n".format(self.url_list[1])
info_headers = "headers = {}\n".format(self.headers)
info_cookies = "cookies = {}\n".format(self.cookies)
info_data = "data = {}\n\n".format(self.data)
if "GET" in self.url_list[0]:
info_req = "html = requests.get(url, headers=headers, verify=False, cookies=cookies)\n"
else:
info_req = "html = requests.post(url, headers=headers, verify=False, cookies=cookies, data=data)\n"
info_end = "print(len(html.text))\nprint(html.text)\n"
text = info_beg + info_url + info_headers + info_cookies + info_data + info_req + info_end
with open(save_name, "w+", encoding="utf8") as p:
p.write(text)
print("转化成功!!")
print(save_name, "文件保存!")
def read_infos(self):
with open(self.str_filename, "r+", encoding="utf-8") as p:
old_line = ""
for line in p:
if old_line == b"\n" and line.encode() == b"\n":
break
old_line = line.encode()
self.text += old_line.decode()
# print("self.text:", self.text)
def start(self):
self.read_infos()
self.get_url()
self.get_headers()
self.get_cookies()
self.get_data()
print("self.url_list:", self.url_list)
print("self.headers:", self.headers)
print("self.cookies:", self.cookies)
print("self.data:", self.data)
self.get_req()
if __name__ == '__main__':
f = FidToPy(str_filename, save_name)
f.start()
说明
使用教程:
首先从Fiddler选中你要转换的请求 -> Save -> Selected Session -> as Text(也可以选择as Text(Header only)...),如图所示:
这样,我们就得到了包含了这个请求的txt文本:
把这个文件移动到和前面Python代码同一个文件夹,然后修改Python中第四行的,改成你请求信息txt文件的名字,如:
str_filename = "15_Full.txt"
右键运行,就可以得到一个同名的.py文件,这个就是我们要的Python请求代码了,so simply!!
效果图:
注:如果是App抓包,post请求中data可能需要加一个解码步骤
浏览器
前言:很多时候我们想要写一个测试请求时,都要一行一行代码写,很不方便,前些天看到一篇Curl转换的代码的介绍,直呼没有早点看到,在无意义的代码上浪费了不少时间(其实最近刚好写了一个Fiddler一键转Python的代码,打算抽空写一个浏览器请求一键转换成python的代码,就看到了这篇博客,哈哈,感谢)。利用好Curl转换,我们可以一键将浏览器上的请求转换成Python代码(不仅仅是Python代码!!),话不多说,上教程。
trillworks网站:https://curl.trillworks.com/#python
首先从浏览器复制请求信息,看截图:
打开前面的网站,复制进去 - 转换 - 完成!
测试:
是不是省了很多事,感谢:https://curl.trillworks.com/#python
如果有其他好用的工具,大家可以一起分享!