环境:
- python3.x版本
- 注册第三方短信平台:榛子云短信(http://smsow.zhenzikj.com)
- 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中查看错误信息
完整代码:
import zhenzismsclient as smsclient
client = smsclient.ZhenziSmsClient('https://sms_developer.zhenzikj.com', 'appId', 'appSecret')
print(client.send('18218299414', '今晚吃鸡'))
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