Python三行代码实现短信发送功能(非twilio)

环境:
  1. python3.x版本
  2. 注册第三方短信平台:榛子云短信(http://smsow.zhenzikj.com)
  3. API文档:http://smsow.zhenzikj.com/doc/python_sdk_doc.html
用法:

1. SDK下载: http://smsow.zhenzikj.com/doc/sdk.html

但是这个官方提供的SDK直接使用是有个Bug的

urllib.error.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:847)>

这个bug, 需要在SDK代码中修改相应代码:

import ssl # 添加
context = ssl._create_unverified_context() # 添加
res_data = urllib.request.urlopen(req, context=context) # 修改

完整代码见最底部

2. 发送信息代码:

import zhenzismsclient as smsclient
# client = smsclient.ZhenziSmsClient(apiUrl, appId, appSecret);
# apiUrl为请求地址,个人开发者使用https://sms_developer.zhenzikj.com,企业开发者使用https://sms.zhenzikj.com
# appId:应用id,可通过用户中心,应用详情查看
# appSecret: 应用秘钥,可通过用户中心,应用详情查看
client = smsclient.ZhenziSmsClient('https://sms_developer.zhenzikj.com', 'appId', 'appSecret') # 需要修改appID, appSecret
print(client.send('18218299414', '今晚吃鸡'))

结果为:

{
	"code":0,
	"data":"发送成功"
}
返回结果是json格式的字符串, 
code: 发送状态,0为成功。非0为发送失败,可从data中查看错误信息
完整代码:

message.py

import zhenzismsclient as smsclient

client = smsclient.ZhenziSmsClient('https://sms_developer.zhenzikj.com', 'appId', 'appSecret')
print(client.send('18218299414', '今晚吃鸡'))

zhenzismsclient.py

import urllib.request
import urllib.parse
import ssl


class ZhenziSmsClient(object):
	def __init__(self, apiUrl, appId, appSecret):
		self.apiUrl = apiUrl
		self.appId = appId
		self.appSecret = appSecret

	def send(self, number, message, messageId=''):
		data = {
			'appId': self.appId,
			'appSecret': self.appSecret,
			'message': message,
			'number': number,
			'messageId': messageId
		}

		data = urllib.parse.urlencode(data).encode('utf-8')
		req = urllib.request.Request(self.apiUrl+'/sms/send.do', data=data)
		context = ssl._create_unverified_context()
		res_data = urllib.request.urlopen(req, context=context)
		res = res_data.read()
		res = res.decode('utf-8')
		return res

	def balance(self):
		data = {
			'appId': self.appId,
			'appSecret': self.appSecret
		}
		data = urllib.parse.urlencode(data).encode('utf-8')
		req = urllib.request.Request(self.apiUrl+'/account/balance.do', data=data)
		res_data = urllib.request.urlopen(req)
		res = res_data.read()
		return res

	def findSmsByMessageId(self, messageId):
		data = {
			'appId': self.appId,
			'appSecret': self.appSecret,
			'messageId': messageId
		}
		data = urllib.parse.urlencode(data).encode('utf-8')
		req = urllib.request.Request(self.apiUrl+'/smslog/findSmsByMessageId.do', data=data)
		res_data = urllib.request.urlopen(req)
		res = res_data.read()
		return res

 
更多内容:

爬虫验证码:破解【点击旋转验证码】
https://blog.csdn.net/weixin_40576010/article/details/89607998
极验3.0滑验证码破解:selenium+计算滑动缺口坐标算法=80%通过率
https://blog.csdn.net/weixin_40576010/article/details/89255933
Linux下利用jTessBoxEditor工具进行Tesseract样本训练【图】
https://blog.csdn.net/weixin_40576010/article/details/89517946
如何获取大量廉价可靠代理IP地址?https://blog.csdn.net/weixin_40576010/article/details/89507924


  • 12
    点赞
  • 77
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值