account & password 注册之后提供 还有10条赠送信息 实验够用了 练习使用仅供参考
import http.client
import urllibdef send_sms(mobile):
host = "106.ihuyi.com"
sms_send_uri = "/webservice/sms.php?method=Submit" # 用户名是登录用户中心->验证码短信->产品总览->APIID
account = "C12345678"
# 密码 查看密码请登录用户中心->验证码短信->产品总览->APIKEY
password = " ******************************************* " message_code = str(random.randint(100000, 999999)) # 生成大于等于100000小于等于999999的一个数
print(message_code)
text = "您的验证码是:" + message_code + "。请不要把验证码泄露给其他人。" # 将整型x转为字符串型并且发送 params = urllib.parse.urlencode(
{'account': account, 'password': password, 'content': text, 'mobile': mobile, 'format': 'json'})
headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}
conn = http.client.HTTPConnection(host, port=80, timeout=30)
conn.request("POST", sms_send_uri, params, headers)
response = conn.getresponse()
response_str = response.read() conn.close()
# 把验证码放进session中 问题是验证完之后就没有作用了
request.session['message_code'] = message_code # 验证之后删除 还不知道具体放哪删除最好 暂且放置在验证环节最后
# del request.session['message_code'] return response_str.decode()
if __name__ == '__main__':
mobile = "183********"
print(send_sms(mobile))