python实现发送和获取手机短信验证码

首先为大家分享python实现发送手机短信验证码后台方法,供大家参考,具体内容如下

1、生成4位数字验证码

def createphonecode(session):
chars=[‘0′,’1′,’2′,’3′,’4′,’5′,’6′,’7′,’8′,’9’]
x = random.choice(chars),random.choice(chars),random.choice(chars),random.choice(chars)
verifycode = “”.join(x)
session[“phoneverifycode”] = {“time”:int(time.time()), “code”:verifycode}
return verifycode

2、发送给外部短信接口(post方式)

def sendtelmsg(msg, phoneid):
sendtelmsgurl=”http://www.810086.com.cn/jk.aspx”
params = {“zh”:”china”, “mm”:”china@10086″,
“hm”:phoneid,”nr”:msg,”sms_type”:88}
postdata=urllib.urlencode(params)
req = urllib2.request(sendtelmsgurl, postdata)
req.add_header(‘content-type’, “application/x-www-form-urlencoded”)
respone = urllib2.urlopen(req)
res = respone.read()
return res

其中session参数是django urls.py 后台方法 以request.session传入

3、前端js

$(“button[name=getverifybt]”).bind(“click”, function(){
var self = this;
var userphoneel = $(“input[name=phonenum]”);
var userphone = $.trim(userphoneel.val());
if (userphone == “”){
alert(“请填写号码!”);
return;
}
$.get(“/getphoneverifycode/”+userphone + “/”)
.success(function(msg){
console.info(msg);
var ddel = $(self).siblings(“dd.showtag”);
if(msg == “ok”){
ddel.find(“span”).hide();
ddel.find(“span[name=success]”).show();
}else{
ddel.find(“span”).hide();
ddel.find(“span[name=error]”).show();
}
})
.error(function(msg){
console.info(msg);
});
var step = 60;
$(this).attr(“disabled”, true);
$(this).html(“重新发送”+step);
var interthread = setinterval(function(){
step-=1;
$(self).html(“重新发送”+step);
if(step

Posted in 未分类

发表评论