首页 > 编程 > Python > 正文

DIY个人智能家庭网关——python篇之推送消息到手机

2019-11-06 09:28:59
字体:
来源:转载
供稿:网友

《使用第三方推送平台JPUSH推送消息到android手机》文章里测试消息推送是在页面上执行的,而现在我需要从路由器上把消息推送出去,打开Jpush API文档,有curl示例命令

参照示例命令,写一个针对自己应用的最简单的测试命令,-u后面的参数是自己应用的AppKey和 Master secret,‘Hi,JPush’是要推送的消息内容

[html] view plain copy 在CODE上查看代码片curl -X POST  -v https://api.jpush.cn/v3/push/ -H "Content-Type: application/json" -u "xxx:xxx" -d '{"platform":"all","audience":"all","notification":{"alert":"Hi,JPush!"}}'  

在路由器上执行这个命令的时候会出错如下错误,证书验证失败,其实是因为curl没有指定证书,这个证书需要到curl官网下载

下载下来后,通过--cacert参数指定

[html] view plain copy 在CODE上查看代码片curl -X POST  --cacert cacert.pem -v https://api.jpush.cn/v3/push/ -H "Content-Type: application/json" -u "xxx:xxx" -d '{"platform":"all","audience":"all","notification":{"alert":"Hi,JPush!"}}'  

OK,手机立马就接收到了

python代码如下

#!/usr/bin/env python# -*- coding: utf-8 -*- import subPRocessimport json   def push_msg(msg):    content = {"platform":"all","audience":"all", "notification":{"alert":msg}}    print content    json_str = json.dumps(content)    print json_str    cmd = "curl -X POST  --cacert /etc/ssl/certs/ca-certificates.crt -v https://api.jpush.cn/v3/push/ -H /"Content-Type: application/json/" -u /"xxx:xxx/""    curl_cmdline = '%s -d /'%s/''%(cmd,json_str)    print curl_cmdline    rc = subprocess.call(curl_cmdline, shell=True);  


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表