騰訊雲短信 redis

開通騰訊雲短信mysql

  一、官網註冊實名帳號:https://cloud.tencent.com
  二、選取短信服務建立短信應用
  三、申請簽名與短信模板 - 經過微信公衆號申請redis

騰訊雲短信二次封裝  libs文件夾下建立txsms包sql

libs/txsms/settings.py      數據庫

# 短信應用 SDK AppID - SDK AppID 以1400開頭
APP_ID = ...
# 短信應用 SDK AppKey
APP_KEY = "..."
# 短信模板ID,須要在短信控制檯中申請
TEMPLATE_ID = ...
# 簽名 - 是`簽名內容`,而不是`簽名ID`
SMS_SIGN= "..."
# 電話前綴
MOBILE_PREFIX = 86

libs/txsms/sms.py  luffyapi終端下  pip install qcloudsms_pydjango

# 經過MacOS ssl安全認證
import ssl
ssl._create_default_https_context = ssl._create_unverified_context

# 獲取驗證碼的功能
import random
def get_code():
    code = ''
    for i in range(4):
        code += str(random.randint(0, 9))
    return code

# 短信發送者
from qcloudsms_py import SmsSingleSender
from .settings import *
sender = SmsSingleSender(APP_ID, APP_KEY)

# 發送驗證碼
from utils.logging import logger
def send_sms(mobile, code, exp):
    try:
        # 發送短信
        response = sender.send_with_param(MOBILE_PREFIX, mobile, TEMPLATE_ID, (code, exp), sign=SMS_SIGN, extend="", ext="")
        # 成功
        if response and response['result'] == 0:
            return True
        # 失敗
        logger.warning('%s - %s' % ('短信發送失敗', response['result']))
    except Exception as e:
        # 異常
        logger.warning('%s - %s' % ('短信發送失敗', e))
    return False

libs/txsms/__init__.pyapi

from .sms import get_code, send_sms

scripts/t_sms.py數組

from libs import txsms
code = txsms.get_code()
print(code)
print(txsms.send_sms('電話', code, 5))

redis數據庫緩存

一、redis是內存 no-sql 數據庫,相比mysql等硬盤數據庫效率高
二、在內存值配置數據庫使用,而不直接使用內存,redis存儲的數據是能夠管理的
三、memcache也是內存數據庫,且django默認採用的就是memcache數據庫,用redis替換memcache的路由很簡單,後者更強大
    redis支持更多的數據類型
    redis自帶緩存機制,出現數據庫系統崩潰數據也是能夠有找回的功能
    redis能夠主動完成數據持久化(自帶數據持久化功能)
    redis的數據過時時間機制也能夠自身完成安全

redis數據類型微信

  支持的數據類型:String、Hash、List、Set、Sorted Set  String:存儲其餘類型不能存的全部數據  Hash:存儲 key-value 形式數據,相似於字典  List:存儲 一系列有序value 形式數據,列表(數組)  Set:存儲 一系列無序value 形式數據,集合  Sorted Set:存儲 有排列標號value 形式數據,排行

相關文章
相關標籤/搜索