您的位置:首页 > 编程语言 > Python开发

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

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



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

[html] view
plain copy

 





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

 





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 subprocess
import 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);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: