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

来源:互联网 发布:银河系漫游指南知乎 编辑:程序博客网 时间:2024/04/29 23:19

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


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

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. 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上查看代码片派生到我的代码片
  1. 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);  



0 0
原创粉丝点击