用过requests写接口的人都知道,当我们抓包查看到url参数/body参数的时候,我们需要将(key1=value&key2=value2)手动打成python数据类型中的字典来进行发动接口请求
一、介绍使用场景
比如下方fiddler抓包界面 大家看参数很多 假设参数很少 那还好 ,但是如果一多,手打成dict岂不是很麻烦?
之前我们都是将参数 手打成dict={'key1':'vlaue1','key2','vlaue2'......}这种格式。现在我们只需要copy上图中这一段 ,之后放在软件中,直接点击转化即可翻译成我们所需要的dicr格式,之后我们在python直接粘贴,这样岂不是很方便?
二、软件概述。
首先介绍一下软件,分为三个作用
a.将key1=value&key2=value2转化成dict格式
b.将key1=value;key2=value2转化成dict格式(fiddler、raw数据格式中,cookie就是这类形式)
c.在线翻译单词,支持多国语言转化(需要联网)
软件会根据你输入的格式,智能判断从而进行不同的功能。
三、软件源代码(python3.6)
a.gui界面代码
import wx
import wx.xrc
###########################################################################
## Class Frame
###########################################################################
class Frame(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, parent, id=wx.ID_ANY, title=u"ThreeToOneTools", pos=wx.DefaultPosition, size=wx.Size(700, 300),
style=wx.DEFAULT_FRAME_STYLE | wx.TAB_TRAVERSAL)
self.icon = wx.Icon('logo.ico', wx.BITMAP_TYPE_ICO)
self.SetIcon(self.icon)
self.SetSizeHintsSz(wx.DefaultSize, wx.DefaultSize)
wxBoxSizer = wx.BoxSizer(wx.VERTICAL)
self.staticTexttitle = wx.StaticText(self, wx.ID_ANY, u"请在第一个输入框输入字符", wx.DefaultPosition, wx.DefaultSize, 0)
self.staticTexttitle.Wrap(-1)
wxBoxSizer.Add(self.staticTexttitle, 0, wx.ALL, 5)
self.CommonInput = wx.TextCtrl(self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.Size(700,25), 0)
wxBoxSizer.Add(self.CommonInput, 0, wx.ALL, 5)
self.ResultInput = wx.TextCtrl(self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.Size(700,25), 0)
wxBoxSizer.Add(self.ResultInput, 0, wx.ALL, 5)
self.BtnTranwords = wx.Button(self, wx.ID_ANY, u"点击此处进行转化", wx.Point(100, 100), wx.DefaultSize, 0)
wxBoxSizer.Add(self.BtnTranwords, 0, wx.ALL, 5)
self.staticTexttitle = wx.StaticText(self, wx.ID_ANY, u"软件使用说明:", wx.DefaultPosition, wx.DefaultSize, 0)
self.staticTexttitle.Wrap(-1)
wxBoxSizer.Add(self.staticTexttitle, 0, wx.ALL, 5)
self.staticTexttitle = wx.StaticText(self, wx.ID_ANY,u"1、输入key1=value1&&key2=value2&&key3=value3格式字符将会转化成Dict", wx.DefaultPosition, wx.DefaultSize, 0)
self.staticTexttitle.Wrap(-1)
wxBoxSizer.Add(self.staticTexttitle, 0, wx.ALL, 5)
self.staticTexttitle = wx.StaticText(self, wx.ID_ANY, u"2、输入key1=value1;key2=value2;key3=value3格式字符将会转化成Dict", wx.DefaultPosition, wx.DefaultSize, 0)
self.staticTexttitle.Wrap(-1)
wxBoxSizer.Add(self.staticTexttitle, 0, wx.ALL, 5)
self.staticTexttitle = wx.StaticText(self, wx.ID_ANY, u"3、如不符合上述格式,将进行联网在线翻译,支持多国语言翻译。", wx.DefaultPosition, wx.DefaultSize, 0)
self.staticTexttitle.Wrap(-1)
wxBoxSizer.Add(self.staticTexttitle, 0, wx.ALL, 5)
self.SetSizer(wxBoxSizer)
self.Layout()
self.Centre(wx.BOTH)
# Connect Events
self.BtnTranwords.Bind(wx.EVT_BUTTON, self.BtnTranWord)
def __del__(self):
pass
# Virtual event handlers, overide them in your derived class
def BtnTranWord(self, event):
event.Skip()
b.主函数界面代码
# -*- coding: UTF-8 -*-
import wx
import BaseTools
import sys
import chardet
import hashlib
import random
import requests
import time
import importlib
import sys
importlib.reload(sys)
q = None
appid = "填你自己的"
SecretKey = "填你自己的"
salt = str(random.random())
q = None
class MianWindow(BaseTools.Frame):
# 首先,咱们从刚刚源文件中将主窗体继承下来.就是修改过name属性的主窗体咯。
def BtnTranWord(self, event):
#1.定义翻译单词函数
def WordFanyi():
def SignMake(q):
# 创建index字符串
index = appid + q + salt + SecretKey
# 对index进行md5加密
Md5 = hashlib.md5()
Md5.update(index.encode("utf-8"))
str = Md5.hexdigest()
return str
q = str(self.CommonInput.GetValue())
if (q.strip() == ""):
self.ResultInput.SetValue("请在第一个输入框输入您需要转化的字符.....")
else:
BaiDuFanYi = {'q': q, 'from': 'auto', 'to': 'auto', 'appid': appid, 'salt': salt, 'sign': SignMake(q)}
try:
BaiDuFanYi = requests.get("http://api.fanyi.baidu.com/api/trans/vip/translate", params=BaiDuFanYi)
try:
self.ResultInput.SetValue(BaiDuFanYi.json()["trans_result"][0]["dst"])
except Exception:
self.ResultInput.SetValue(BaiDuFanYi.json()["error_msg"] + "错误码:" + BaiDuFanYi.json()["error_code"])
except Exception as EX:
self.ResultInput.SetValue("请求失败...检查电脑网络是否通畅")
#2.定义cookie格式转Dict
def CookieTranDict():
strcookie = str(self.CommonInput.GetValue())
strlen = len(strcookie.split(";"))
if(strlen==1):self.ResultInput.SetValue("非Cookie格式,请重新输入....")
else:
DictNum1List = []
DictNum2List = []
DictBody = {}
for i in range(0, strlen):
try:
DictNum1 = strcookie.split(";")[i].strip().split("=")[0]
DictNum2 = strcookie.split(";")[i].strip().split("=")[1]
DictNum1List.append(DictNum1)
DictNum2List.append(DictNum2)
for x in range(0, len(DictNum1List)):
DictBody[DictNum1List[x]] = DictNum2List[x]
DictStrRaw = ""
DictStr = ""
for key in DictBody.keys():
values = (key, DictBody[key])
DictStrRaw = DictStrRaw + "," + "'%s'" ':' "'%s'" % (values)
DictStr = "{" + DictStrRaw[1:] + "}"
self.ResultInput.SetValue(DictStr)
except Exception as Ex:
self.ResultInput.SetValue("非Cookie格式,请重新输入....")
#3.定义url格式转换成dict
def UrlTranDict():
strcookie = str(self.CommonInput.GetValue())
strlen = len(strcookie.split("&"))
if(strlen==1): self.ResultInput.SetValue("非Url参数格式,请重新输入....")
else:
DictNum1List = []
DictNum2List = []
DictBody = {}
for i in range(0, strlen):
try:
DictNum1 = strcookie.split("&")[i].strip().split("=")[0]
DictNum2 = strcookie.split("&")[i].strip().split("=")[1]
DictNum1List.append(DictNum1)
DictNum2List.append(DictNum2)
for x in range(0, len(DictNum1List)):
DictBody[DictNum1List[x]] = DictNum2List[x]
DictStrRaw = ""
DictStr = ""
#将字典拼接成Str
for key in DictBody.keys():
values = (key, DictBody[key])
DictStrRaw = DictStrRaw + "," + "'%s'" ':' "'%s'" % (values)
DictStr = "{" + DictStrRaw[1:] + "}"
self.ResultInput.SetValue(DictStr)
except Exception as Ex:
self.ResultInput.SetValue("非URl参数格式,请重新输入....")
#根据输入的字符串格式来调用不同的方法
strword = str(self.CommonInput.GetValue())
douhao = ";"
dengyu = "="
yufuhao = "&"
if douhao in strword and dengyu in strword:
CookieTranDict()
elif yufuhao in strword and dengyu in strword:
UrlTranDict()
else:
WordFanyi()
if __name__ == '__main__':
app = wx.App()
main_win = MianWindow(None)
main_win.Show()
app.MainLoop()
四、软件运行
a.复制fiddler中cookie进行转化
b.复制url参数(复制url"?"后面的的键值对参数 进行转化即可)或者Body参数进行转化 比如下图
c.如果输入的字符格式不属于上面的,就会调用翻译接口,支持多国各种语言翻译(比如英文,葡萄牙语,西班牙语)
最后分析一份软件给大家,百度云下载链接:https://pan.baidu.com/s/1Sh1w1R6EpJlGB3bJuVH4-g 密码:lyel。
解压之后,点击上图圈记文件,即可创建快捷方式到桌面(360/或者腾讯管家报毒,点击允许即可),之后点击桌面创建的图标即可使用此软件了,原创不易,觉得实用的请多多转发, 本人接口小白,微信17673231234,欢迎学习接口测试的可以加我,一起探讨。。。
使用几天之后,小编发现所使用的是单行文本,所以当复制多行文字进行翻译的时候,程序只会取第一个数据,所以更新了一波
BaseTools.py代码
# -*- coding: utf-8 -*-
###########################################################################
## Python code generated with wxFormBuilder (version Jun 17 2015)
## http://www.wxformbuilder.org/
##
## PLEASE DO "NOT" EDIT THIS FILE!
###########################################################################
import wx
import wx.xrc
###########################################################################
## Class Frame
###########################################################################
class Frame(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, parent, id=wx.ID_ANY, title=u"ThreeToOneTools", pos=wx.DefaultPosition, size=wx.Size(500, 700),
style=wx.DEFAULT_FRAME_STYLE | wx.TAB_TRAVERSAL)
self.icon = wx.Icon('logo.ico', wx.BITMAP_TYPE_ICO)
self.SetIcon(self.icon)
self.SetSizeHintsSz(wx.DefaultSize, wx.DefaultSize)
wxBoxSizer = wx.BoxSizer(wx.VERTICAL)
self.staticTexttitle = wx.StaticText(self, wx.ID_ANY, u"请在第一个输入框输入字符", wx.DefaultPosition, wx.DefaultSize, 0)
self.staticTexttitle.Wrap(-1)
wxBoxSizer.Add(self.staticTexttitle, 0, wx.ALL, 5)
self.CommonInput = wx.TextCtrl(self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.Size(500,200), wx.TE_MULTILINE|wx.TE_RICH|wx.TE_PROCESS_ENTER)
wxBoxSizer.Add(self.CommonInput, 0, wx.ALL, 5)
self.staticTexttitleView = wx.StaticText(self, wx.ID_ANY,u"感谢您使用此软件", wx.DefaultPosition, wx.DefaultSize, 0)
self.staticTexttitleView.Wrap(-1)
wxBoxSizer.Add(self.staticTexttitleView, 0, wx.ALL, 5)
self.ResultInput = wx.TextCtrl(self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.Size(500,200), wx.TE_MULTILINE|wx.TE_RICH|wx.TE_PROCESS_ENTER)
wxBoxSizer.Add(self.ResultInput, 0, wx.ALL, 5)
self.BtnTranwords = wx.Button(self, wx.ID_ANY, u"点击此处进行转化", wx.Point(100, 100), wx.DefaultSize, 0)
wxBoxSizer.Add(self.BtnTranwords, 0, wx.ALL, 5)
self.staticTexttitle = wx.StaticText(self, wx.ID_ANY, u"软件使用说明:", wx.DefaultPosition, wx.DefaultSize, 0)
self.staticTexttitle.Wrap(-1)
wxBoxSizer.Add(self.staticTexttitle, 0, wx.ALL, 5)
self.staticTexttitle = wx.StaticText(self, wx.ID_ANY,u"1、输入key1=value1&&key2=value2&&key3=value3格式字符将会转化成Dict", wx.DefaultPosition, wx.DefaultSize, 0)
self.staticTexttitle.Wrap(-1)
wxBoxSizer.Add(self.staticTexttitle, 0, wx.ALL, 5)
self.staticTexttitle = wx.StaticText(self, wx.ID_ANY, u"2、输入key1=value1;key2=value2;key3=value3格式字符将会转化成Dict", wx.DefaultPosition, wx.DefaultSize, 0)
self.staticTexttitle.Wrap(-1)
wxBoxSizer.Add(self.staticTexttitle, 0, wx.ALL, 5)
self.staticTexttitle = wx.StaticText(self, wx.ID_ANY, u"3、如不符合上述格式,将进行联网在线翻译,支持多国语言翻译。", wx.DefaultPosition, wx.DefaultSize, 0)
self.staticTexttitle.Wrap(-1)
wxBoxSizer.Add(self.staticTexttitle, 0, wx.ALL, 5)
self.SetSizer(wxBoxSizer)
self.Layout()
self.Centre(wx.BOTH)
# Connect Events
self.BtnTranwords.Bind(wx.EVT_BUTTON, self.BtnTranWord)
def __del__(self):
pass
# Virtual event handlers, overide them in your derived class
def BtnTranWord(self, event):
event.Skip()
MainTools.py代码
# -*- coding: UTF-8 -*-
import wx
import BaseTools
import sys
import chardet
import hashlib
import random
import requests
import time
import importlib
import sys
importlib.reload(sys)
q = None
appid = "填你自己的"
SecretKey = "填你自己的"
salt = str(random.random())
q = None
class MianWindow(BaseTools.Frame):
# 首先,咱们从刚刚源文件中将主窗体继承下来.就是修改过name属性的主窗体咯。
def BtnTranWord(self, event):
#1.定义翻译单词函数
def WordFanyi():
def SignMake(q):
# 创建index字符串
index = appid + q + salt + SecretKey
# 对index进行md5加密
Md5 = hashlib.md5()
Md5.update(index.encode("utf-8"))
str = Md5.hexdigest()
return str
q = str(self.CommonInput.GetValue())
if (q.strip() == ""):
self.ResultInput.SetValue("请在第一个输入框输入您需要转化的字符.....")
else:
BaiDuFanYi = {'q': q, 'from': 'auto', 'to': 'auto', 'appid': appid, 'salt': salt, 'sign': SignMake(q)}
try:
self.ResultInput.SetValue("")
BaiDuFanYi = requests.get("http://api.fanyi.baidu.com/api/trans/vip/translate", params=BaiDuFanYi)
try:
for i in range(0,len(BaiDuFanYi.json()["trans_result"])):#处理输入的翻译字符是多行的
# 追加到末尾 但是不换行ApppendText
self.ResultInput.AppendText(BaiDuFanYi.json()["trans_result"][i]["dst"]+"\n")
CommonInputline=str(self.ResultInput.GetNumberOfLines()-1)#默认会有一个回车行,需要减掉
self.staticTexttitleView.SetLabel("翻译如下,合计:\""+CommonInputline+"\"行,感谢您的使用...")
except Exception as xx:
print(xx)
self.ResultInput.SetValue(BaiDuFanYi.json()["error_msg"] + "错误码:" + BaiDuFanYi.json()["error_code"])
except Exception as EX:
print(EX)
self.ResultInput.SetValue("请求失败...检查电脑网络是否通畅")
#2.定义cookie格式转Dict
def CookieTranDict():
strcookie = str(self.CommonInput.GetValue())
strlen = len(strcookie.split(";"))
if(strlen==1):self.ResultInput.SetValue("非Cookie格式,请重新输入....")
else:
DictNum1List = []
DictNum2List = []
DictBody = {}
for i in range(0, strlen):
try:
DictNum1 = strcookie.split(";")[i].strip().split("=")[0]
DictNum2 = strcookie.split(";")[i].strip().split("=")[1]
DictNum1List.append(DictNum1)
DictNum2List.append(DictNum2)
for x in range(0, len(DictNum1List)):
DictBody[DictNum1List[x]] = DictNum2List[x]
DictStrRaw = ""
DictStr = ""
for key in DictBody.keys():
values = (key, DictBody[key])
DictStrRaw = DictStrRaw + "," + "'%s'" ':' "'%s'" % (values)
DictStr = "{" + DictStrRaw[1:] + "}"
self.staticTexttitleView.SetLabel("cookie格式转化Dict完成,请直接copy进行使用")
self.ResultInput.SetValue(DictStr)
except Exception as Ex:
self.ResultInput.SetValue("非Cookie格式,请重新输入....")
#3.定义url格式转换成dict
def UrlTranDict():
strcookie = str(self.CommonInput.GetValue())
strlen = len(strcookie.split("&"))
if(strlen==1): self.ResultInput.SetValue("非Url参数格式,请重新输入....")
else:
DictNum1List = []
DictNum2List = []
DictBody = {}
for i in range(0, strlen):
try:
DictNum1 = strcookie.split("&")[i].strip().split("=")[0]
DictNum2 = strcookie.split("&")[i].strip().split("=")[1]
DictNum1List.append(DictNum1)
DictNum2List.append(DictNum2)
for x in range(0, len(DictNum1List)):
DictBody[DictNum1List[x]] = DictNum2List[x]
DictStrRaw = ""
DictStr = ""
#将字典拼接成Str
print(DictBody)
for key in DictBody.keys():
values = (key, DictBody[key])
DictStrRaw = DictStrRaw + "," + "'%s'" ':' "'%s'" % (values)
DictStr = "{" + DictStrRaw[1:] + "}"
self.ResultInput.SetValue(DictStr)
self.staticTexttitleView.SetLabel("URL格式转化Dict完成,请直接copy进行使用")
except Exception as Ex:
self.ResultInput.SetValue("非URl参数格式,请重新输入....")
#根据输入的字符串格式来调用不同的方法
strword = str(self.CommonInput.GetValue())
douhao = ";"
dengyu = "="
yufuhao = "&"
if douhao in strword and dengyu in strword:
CookieTranDict()
elif yufuhao in strword and dengyu in strword:
UrlTranDict()
else:
WordFanyi()
if __name__ == '__main__':
app = wx.App()
main_win = MianWindow(None)
main_win.Show()
app.MainLoop()
翻译界面截图。
url转化dict界面截图
cookie转化dict界面截图
最后ThreeToOneV1.1.zip 新版本下载地址:链接:https://pan.baidu.com/s/1meiv2CwTgYDQuFTg7Pv77Q 密码:lrb7
时隔多日 无聊中 对这个软件功能增加了对fiddler中的Header进行快速转化成dict格式 ,如下
这个只适合copy header部分 对于cookie请单独进行转化
好了下面是转化的运行图 直接copy下面的就可以使用了 (这里对key和value进行了所有的空格去除 所以首尾和中间都是没有空格)
新版本下载地址
这里如果参数值包含分隔符的话 是不起作用的 所以本工具只能转换百分之就是左右的格式。
链接:https://pan.baidu.com/s/1c47-QYneC-zFxYk96lZI0w
提取码:apy3
复制这段内容后打开百度网盘手机App,操作更方便哦