人生苦短,我用python。

一、hashlib模块

MD5加密请求接口过程:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# MD5加密

import requests
import json
import hashlib
import time

headers = {'userAgent':'Xclvg/1.8.0 (Linux; U; Android 5.0; FN2016 Build)'}

timestamp = int(time.time()*1000) # 时间戳要13位,先乘以1000再转换为int即可
print("timestamp: ", timestamp, "\n")

partnerId="X160923144687829"
ROM_KEY = "8de7bs35c1ag2b54gd70de8495dd0" # ROM_KEY是固定值
token_str = partnerId+str(timestamp)+ROM_KEY # token的字符串拼接
token_bytes_utf8 = token_str.encode(encoding='utf-8') # 进行MD5加密需要encode
m = hashlib.md5() # 定义加密对象
m.update(token_bytes_utf8) # MD5加密
token_md5 = m.hexdigest() # 生成16进制MD5加密
print("token_md5: ", token_md5, "\n")

data = {
"p":"X160923144687829", # pid
"t":str(timestamp), # time
"k":token_md5, # token
}
res = requests.post('http://api.mmvd.kkc.tv/partner/token/getkey.json', data=data, headers=headers) # 请求接口获得key值
key = res.text
print("key:", key, "\n")

# sign_str字符串拼接
sign_str = "device_code=4SD334SDF&device_mac=41:F2:09:D3:A6:78&device_sn=DJK38943J429942J2"+"&timestamp="+str(timestamp)+"&user_uuid=25&version=V-1.1-2.02.78"+"&key="+key
print("sign_str: ", sign_str, "\n")
sign_bytes_utf8 = sign_str.encode(encoding='utf-8')
md5 = hashlib.md5()
md5.update(sign_bytes_utf8)
sign_md5 = md5.hexdigest().upper() # sign的MD5加密字符串需要大写
print("sign_md5: ", sign_md5, "\n")

params = {
"device_code":"4SD334SDF",
"device_mac":"41:F2:09:D3:A6:78",
"device_sn":"DJK38943J429942J2",
"sign":sign_md5,
"timestamp":str(timestamp),
"user_uuid":"25",
"version":"V-1.1-2.02.78"
}

response = requests.post('http://api.mmvd.kkc.tv/pay/query.json', data=params, headers=headers) # 请求接口获得钱包信息

print("status_code: ", response.status_code, "\n")
print("response: ", response.json(), "\n")

持续更新…

最后更新: 2018年12月05日 10:27

原始链接: http://pythonfood.github.io/2017/12/30/pythonMD5加密/

× 多少都行~
打赏二维码