import requests
import json
import random
import urllib
import time
import base64
import hmac
from hashlib import sha1
def hash_hmac(key, code, sha1):
hmac_code = hmac.new(key.encode(), code.encode(), sha1)
return hmac_code.digest()
def send():
url = 'https://api.yisu.com/sms/sendSms'
accessId = '*****'
accessSecret = '*****'
params = {
'accessId': accessId,
'templateCode' : 100001,
'nonce': random.randint(10000000, 99999999),
'phone' : '13800000000',
'timestamp' : int(time.time()),
'templateVars' : json.dumps({"code":"123123","min":5}, separators=(',', ':'))
}
params_keys = list(params.keys())
params_keys.sort()
params_str = ""
for key in params_keys:
params_str += key + '=' + str(params[key]) + '&'
params_str = params_str[:-1]
sign = base64.b64encode(hash_hmac(accessSecret, params_str, sha1))
params['signature'] = str(sign,'utf-8')
res = requests.post(url, data=params, verify=False)
print(res.text)
if __name__ == '__main__':
send()