图形验证码逻辑分析
图形验证码后端逻辑
准备captcha扩展包
captcha扩展包用于后端生成图形验证码
图形验证码后端逻辑实现
1 @api.route('/image_codes/
2 def get_image_code(image_code_id):
3 """
4 获取图片验证码
5 :param image_code_id: 图片验证码编号
6 :return: 返回验证码图片
7 """
8 text, image_data = captcha.generate_captcha()
9 try:
10 redis_store.setex("image_code_%s" % image_code_id, constants.IMAGE_CODE_REDIS_EXPIRES, text)
11 except Exception as e:
12 # 记录日志
13 logging.error(e)
14 # return jsonify(errno=RET.DBERR, errmsg="save image code id failed")
15 return jsonify(errno=RET.DBERR, errmsg="保存图片验证码失败")
16 # 返回值
17 response = make_response(image_data)
18 response.headers["Content-Type"] = "image/jpg"
19 return response
容联云通讯短信平台
容联云通讯短信平台介绍
容联云通讯网址:https://www.yuntongxun.com/
容联云管理控制台
容联云创建应用
容联云通讯Python SDK
https://doc.yuntongxun.com/p/5f029ae7a80948a1006e776e
1 from ronglian_sms_sdk import SmsSDK
2
3 accId = '容联云通讯分配的主账号ID'
4 accToken = '容联云通讯分配的主账号TOKEN'
5 appId = '容联云通讯分配的应用ID'
6
7 def send_message():
8 sdk = SmsSDK(accId, accToken, appId)
9 tid = '容联云通讯创建的模板ID'
10 mobile = '手机号1,手机号2'
11 datas = ('变量1', '变量2')
12 resp = sdk.sendMessage(tid, mobile, datas)
13 print(resp)
封装发送短信单例类
封装发送短信单例类
1 class CCP(object):
2 """发送短信的单例类"""
3
4 def __new__(cls, *args, **kwargs):
5 # 判断是否存在类属性_instance,_instance是类CCP的唯一对象,即单例
6 if not hasattr(cls, "_instance"):
7 cls._instance = super(CCP, cls).__new__(cls, *args, **kwargs)
8 cls._instance.rest = SmsSDK(accId, accToken, appId)
9 return cls._instance
封装发送短信单例方法
1 def send_message(self, mobile, datas, tid):
2 sdk = self._instance.rest
3 resp = sdk.sendMessage(tid, mobile, datas)
4 result = json.loads(resp)
5 if result['statusCode'] == '000000':
6 return 0
7 else:
8 return 1
测试单例类发送模板短信结果
1 if __name__ == '__main__':
2 d = CCP()
3 d.send_message('18xxxxxxx', ('1234', 5), 1)