python+网易云信API发送短信

# coding=utf-8
import hashlib
import time
import requests


def send_code(mobile):
    url = 'https://api.netease.im/sms/sendcode.action'
    """
    AppKey	开发者平台分配的appkey
    Nonce	随机数(最大长度128个字符)
    CurTime	当前UTC时间戳,从1970年1月1日0点0 分0 秒开始到现在的秒数(String)
    CheckSum	SHA1(AppSecret + Nonce + CurTime),三个参数拼接的字符串,进行SHA1哈希计算,转化成16进制字符(String,小写)
    """
    appkey = "***************"
    # 生成128个长度以内的随机字符串
    nonce = hashlib.new('sha512',str(time.time()).encode("utf-8")).hexdigest()
    # 获取当前时间戳
    curtime = str(int(time.time()))
    # 网易云信的 App Secret
    serct = "**************"
    # 根据要求进行SHA1哈希计算
    check_sum = hashlib.sha1((serct + nonce + curtime).encode("utf-8")).hexdigest()

    print(nonce,"\n",len(nonce))
    print(curtime)
    print(check_sum)


    header = {
        "AppKey":appkey,
        "Nonce":nonce,
        "CurTime":curtime,
        "CheckSum":check_sum
    }

    data = {
        'mobile': mobile,  # 你的手机号码
    }

    resp = requests.post(url,data=data,headers=header)

    print("Response:",resp.content)


if __name__ == '__main__':
    send_code("接受短信的手机号")




你可能感兴趣的:(Python)