通过接口给你的 mac 推送消息

mac 下有个 osascript -e 'display notification "title" with title "title"' 的命令可以往推送中心发消息。
配合python脚本来监听端口,然后再找一台外网把请求转发到内网本机就OK了。效果如图

msg@2x

脚本如下

#!/usr/bin/python
#coding=utf-8

__author__ = 'baozi'

from BaseHTTPServer import HTTPServer
import os
from sys import version as python_version

if python_version.startswith('3'):
    from urllib.parse import parse_qs
    from http.server import BaseHTTPRequestHandler
else:
    from urlparse import parse_qs
    from BaseHTTPServer import BaseHTTPRequestHandler

class NoticeHandler(BaseHTTPRequestHandler):
    def do_GET(self):
        query = self.path.split('?',1)
        if len(query) < 2:
            str = 'err'
        else:
            str = 'ok'
            params = parse_qs(query[1])
            title = params.get('title', '')[0]
            msg = params.get('msg', '')[0]
            os.system('osascript -e \'display notification "%s" with title "%s"\'' % (msg, title))

        self.send_response(200)
        self.end_headers()
        self.wfile.write(str)

    def do_POST(self):
        self.wfile.write('method error.')


server = HTTPServer(('', 8001), NoticeHandler)
print 'started http server...'
server.serve_forever()

# 通过外网主机转发 ssh -N -f -R 8001:127.0.0.1:8001 root@外网ip 
# http://外网ip:8001/?title=this%20is%20a%20title&msg=this%20is%20a%20message
分类 未分类

发表评论