python sha256 键值对参数格式接口测试

# coding=utf-8

import requests
import time
import uuid
import hashlib
import hmac
import random
import urllib3
import threading
from config import read_yaml

urllib3.disable_warnings()


def generate_orders_all():
    # 时间戳
    millis = "%d" % (time.time() * 1000)

    # 订单号
    order_id = ''.join((str(uuid.uuid1()).split('-')))

    head_str = ''
    for i in head_data.items():
        head_str += i[0] + '=' + i[1] + '&'
    head_str = head_str[:-1]
body_str = '' for i in body_data.items(): body_str += i[0] + '=' + i[1] + '&' body_str = body_str[:-1]
dict_data = {} dict_data.update(head_data) dict_data.update(body_data)


# key_1=value_1&key_2=value_2格式 dict_data_str = '' for k in sorted(dict_data): dict_data_str += k + '=' + dict_data[k] + '&' dict_data_str = dict_data_str[:-1]

# sha256加密并转成大写 signature_str = hmac.new(secret.encode('utf-8'), dict_data_str.encode('utf-8'), digestmod=hashlib.sha256).digest() signature = signature_str.hex().upper() param_rq = head_str + '&signature=' + signature print('加密后:'+param_rq) head = {"Content-Type": "application/x-www-form-urlencoded"} try: req = requests.post(url=url + '?' + param_rq, data=body_data, headers=head) print('请求url:'+req.url) print('返回结果:' + req.text) except Exception as msg: print('ERROR' + str(msg))

 

你可能感兴趣的:(python sha256 键值对参数格式接口测试)