脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - django项目中使用云片网发送短信验证码的实现

django项目中使用云片网发送短信验证码的实现

2021-08-26 00:42专职 Python

这篇文章主要介绍了django项目中使用云片网发送短信验证码的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

1. 在apps包下新建一个utils的python包

2. utils包中新建一个YunPian.py文件,文件中代码如下

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import requests
import json
def send_single_sms(apikey, code, mobile):
 # 发送单条短信
 url = "https://sms.yunpian.com/v2/sms/single_send.json"
 text = "【后端学习】您的验证码是{}。如非本人操作,请忽略本短信".format(code)
 
 res = requests.post(url, data={
  "apikey": apikey,
  "mobile": mobile,
  "text": text
 })
 
 return res
 
 
if __name__ == '__main__':
 res = send_single_sms("cdc06fa3370dfdsadasffadfadc53dc9d", "149805", "18889565149")
 res_json = json.loads(res.text)
 code = res_json["code"]
 msg = res_json["msg"]
 
 if code == 0:
  print("发送成功")
 else:
  print("发送失败:{}".format(msg))
 
 print(res.text)

3. 云片网发送单条短信的api官网:https://www.yunpian.com/official/document/sms/zh_CN/domestic_single_send

注意:python开发环境中需要下载requests库: pip install requests

到此这篇关于django项目中使用云片网发送短信验证码的实现的文章就介绍到这了,更多相关django 云片网发送验证码内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/weixin_42289273/article/details/112706344

延伸 · 阅读

精彩推荐