from ronglian_sms_sdk import SmsSDK
from flask import current_app
import json
import redis
class RongLian:
sdk = None
red = None
def __init__(self):
accid = current_app.config['ACCOUNT_SID']
appid = current_app.config['APPID']
acctoken = current_app.config['AUTH_TOKEN']
self.red = redis.Redis(password=123,host="127.0.0.1",port=6379)
self.sdk = SmsSDK(accId=accid, accToken=acctoken, appId=appid)
def send_code(self,mobile,datas):
tid = current_app.config['TID']
send_sms = self.sdk.sendMessage(tid=tid, mobile=mobile, datas=datas)
jie = json.loads(send_sms)
key = "send_code_" + mobile
self.red.set(key,datas[0],ex=600)
if jie['statusCode'] == "000000":
print("发送短信成功")
return True
else:
print("发送短信失败")
return False
def check_code(self,code,mobile):
key = "send_code_" + mobile
print(key)
try:
print("3123213123213213", self.red)
p = self.red.get(key)
except Exception as e:
print('hhhhhhhhhhhhhh',e)
return False
print("pppppppppppp",p)
r = p.decode()
if r == str(code):
print("短信验证码验证成功")
return True
else:
print("短信验证码验证失败")
return False
if __name__ == '__main__':
from app import app
with app.app_context():
r = RongLian()
# s = r.send_code("手机号",("2222",10))
# print("eeeee",s)
c = r.check_code("2222","15176058352")
print("cccc",c)
python (Flask、Django) + 容联云
最新推荐文章于 2022-12-04 07:20:17 发布