首页 > 编程 > Python > 正文

DIY个人智能家庭网关——python篇之推送手机上下线事件

2019-11-06 09:28:34
字体:
来源:转载
供稿:网友
#!/usr/bin/env python# -*- coding: utf-8 -*- import timeimport subPRocessfrom time import sleepimport jsonmac = "a8:1b:5a:b7:e0:03"last_state = ""msg = ""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 /"e5068f518ccb04cc92ac601f:9dd7a8d37bbfa2814648a6c5/""    curl_cmdline = '%s -d /'%s/''%(cmd,json_str)    print curl_cmdline    rc = subprocess.call(curl_cmdline, shell=True);     cmd = 'iw dev wlan0 station dump | grep Station'while True:  obj = subprocess.Popen(cmd,stdout=subprocess.PipE,stderr=subprocess.PIPE,shell=True)  obj.wait()  state = 'offline'  lines = obj.stdout.readlines()  for str in lines:										  	if str.find(mac) > 0:  		state = 'online'  		break  if last_state != state:    if state == 'online':      msg = "Welcome home!"    else:      msg = "Goodbye"    push_msg(msg)  last_state = state  time.sleep(2)


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