import hashlib
import base64
key = '123456'
class Sign:
@classmethod
def get_sign(cls, data):
# 不改变传入的data=>直接对字典进行复制
data = data.copy()
# 首先判断字典是否空,为空直接加密
if not data:
string = ''
else:
# 非空字典,过滤value为空和嵌套字典、列表
for k, v in list(data.items()):
if isinstance(v,str):
v = v.replace(" ", "").replace("\n", "")
data[k] = v
if v == '' or v is None or isinstance(v,(dict,list)):
data.pop(k)
# 对字典进行ASCII码排序
new_list = sorted(data.items())
alist = ['&' + str(i[0]) + '=' + str(i[1]) for i in new_list]
string = ''.join(alist)
return {"X-Sign": cls.encry(string)}
@classmethod
def encry(cls, string):
"""
加密算法
:param string: 要加密的字符串
:return: 字符串
"""
if string:
sign = string[1:] + '&key=' + key
else:
sign = 'key=' + key
m = hashlib.md5()
m.update(sign.encode("utf8"))
encodeStr = m.hexdigest()
base_code = base64.b64encode(encodeStr.encode('utf-8'))
return base_code.decode()
import requests
data ={
"mercId": "888000000000001",
"sysCnl": "IOS",
"orderTypes": [0, 1, 2, 3, 4, 5, 6, 7, 9, 10],
"platform": "LXMALL",
"limit": 20,
"page": 1,
"timestamp": "1625881419"
}
sign_data = Sign.get_sign(data)
headers = {
"X-Token" : "08fa6dad125d30842c58d9bea6059ace",
"X-APPVer": "1.5",
"X-SignVer": "v1",
"Content-Type": "application/json"
}
headers |= sign_data
url = base_url + 'order/list'
r = requests.post(url,json = data,headers = headers)
print(r.json())
欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) | Powered by Discuz! X3.2 |