import hashlib
data = {
"app_key": "25617551",
"fields": "cid,parent_cid,name,is_parent",
"format": "json",
"method ": "taobao.item.seller.get",
"parent_cid": "0",
"sign_method ": "md5",
"timestamp": "2019-06-21 13:05:00",
"v": "2.0",
"sign": "AFB4A78BCE0FDB5EFE119ADD9A39762A"
}
class SignatureAndVerification(object):
"""MD5签名和验签"""
@classmethod
def data_processing(cls, data):
"""
:param data: 需要签名的数据,字典类型
:return: 处理后的字符串,格式为:参数名称=参数值,并连接
"""
if "sign" in data:
del data["sign"]
if "sign_type" in data:
del data["sign_type"]
dataList = []
for key in sorted(data):
if data[key]:
dataList.append("%s%s" % (key, data[key]))
return "".join(dataList).strip()
@classmethod
def md5_sign(cls, data):
"""
MD5签名
:param api_key: MD5签名需要的字符串
:return: 签名后的字符串sign
"""
data = cls.data_processing(data) # + api_key.strip()
md5 = hashlib.md5()
md5.update(data.encode(encoding='UTF-8'))
return md5.hexdigest()
@classmethod
def md5_verify(cls, data, signature):
"""
md5验签
:param data: 接收到的数据
:param signature: 接收到的sign
:return: 验签结果,布尔值
"""
if cls.md5_sign(data) == signature:
return True
else:
return False
if __name__ == '__main__':
SignatureAndVerification.data_processing(data)
SignatureAndVerification.md5_sign(data)
# -*- coding: utf-8 -*-
import requests
import json
from common.md5_sign import SignatureAndVerification
def productlist():
url = 'http://192.168.106.143:8089/product/tb/items/get?'
data = {
"sessionId": "",
"pageNo": "1",
"pageSize": "1",
"keyWord": "女装",
"sign": ""
}
SignatureAndVerification.data_processing(data) # 按ASCII码排序
sgin = SignatureAndVerification.md5_sign(data) # md5加密
data['sign'] = sgin
r = requests.post(url, data=data)
rr = json.dumps(json.loads(r.text), indent=2, ensure_ascii=False)
print(rr)
def productinfo():
url = 'http://192.168.106.143:8089/product/tb/info/get?'
data = {
"sessionId": "25617551",
"numIids": "591727331053",
"sign": ""
}
SignatureAndVerification.data_processing(data) # 按ASCII码排序
sgin = SignatureAndVerification.md5_sign(data) # md5加密
data['sign'] = sgin
r = requests.post(url, data=data)
rr = json.dumps(json.loads(r.text), indent=2, ensure_ascii=False)
print(rr)