python实现在 Mac 10.9 远程桌面截屏抓取

来源:互联网 发布:淘宝补单 编辑:程序博客网 时间:2024/06/05 12:05

照样画葫芦,用python编写了一段小程序,可以使用ipad的web浏览器远程监控远端iMac主机界面(每秒截屏,非流控),与大家分享。


1. 首先介绍一下需要下载的第三方工具:

Flask,Pyscreenshot

Flask用来做web服务器,Pyscreenshot是用来截屏的。用pip install 分别安装即可


2. 介绍程序文件架构如下,需要simplesvr.py主伺服程序,和供客户端浏览调用的index.html


3. index.html文件如下:

<span style="font-size:18px;"><!DOCTYPE html><html>    <head>        <meta charset="UTF-8">        <title>Remote Desktop</title>        <style>            body { margin: 0 }            img { max-width: 100% }        </style>    </head>    <body>        <img src="/desktop.jpeg">        <script src="//code.jquery.com/jquery-1.11.1.js"></script>        <script>            function reload_desktop() {                $('img').remove()                $('<img>', {src: '/desktop.jpeg?' +                            Date.now()}).appendTo('body')            }            setInterval(reload_desktop, 2000)            function send_click(event) {                var fac = this.naturalWidth / this.width                $.get('/click', {x: 0|fac * event.clientX,                                 y: 0|fac * event.clientY})            }            $('body').on('click', 'img', send_click)        </script>    </body></html></span>

4.simplesvr.py编写如下:

from flask import Flaskfrom flask import send_filefrom flask import requestimport sysfrom StringIO import StringIOimport pyscreenshotfrom Quartz.CoreGraphics import CGEventCreateMouseEventfrom Quartz.CoreGraphics import CGEventPostfrom Quartz.CoreGraphics import kCGEventMouseMovedfrom Quartz.CoreGraphics import kCGEventLeftMouseDownfrom Quartz.CoreGraphics import kCGEventLeftMouseDownfrom Quartz.CoreGraphics import kCGEventLeftMouseUpfrom Quartz.CoreGraphics import kCGMouseButtonLeftfrom Quartz.CoreGraphics import kCGHIDEventTapapp = Flask(__name__)@app.route('/')def index():    return app.send_static_file('index.html')def mouseEvent(type, posx, posy):        theEvent = CGEventCreateMouseEvent(None, type, (posx,posy), kCGMouseButtonLeft)        CGEventPost(kCGHIDEventTap, theEvent)def mousemove(posx,posy):        mouseEvent(kCGEventMouseMoved, posx,posy);def mouseclick(posx,posy):        #mouseEvent(kCGEventMouseMoved, posx,posy); #uncomment this line if you want to force the mouse to MOVE to the click location first (i found it was not necesary).        mouseEvent(kCGEventLeftMouseDown, posx,posy);        mouseEvent(kCGEventLeftMouseUp, posx,posy);@app.route('/desktop.jpeg')def desktop():       screen = pyscreenshot.grab()       buf = StringIO()       screen.save(buf, 'JPEG', quality=75)       buf.seek(0)       return send_file(buf, mimetype='image/jpeg')@app.route('/click')def click():       try:           x = int(request.args.get('x'))           y = int(request.args.get('y'))       except TypeError:           return 'error'       mouseclick(x, y);       return 'done'if __name__ == '__main__':       app.run(host='0.0.0.0', port=7080, debug=True)


5.启动服务器:python simplesvr.py

6.打开ipad上的safari,输入http://<your server ip address>:7080/, 看到截屏了吧,而且是不断再刷新显示的哦!!


(完)


喜爱python,就行动起来吧~


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 衣服上有502胶水怎么办 衣服上面粘了胶怎么办 衣服上的胶干了怎么办 凌晨4点到火车站怎么办 运管罚款没钱交怎么办 郑州地铁票没买怎么办 遇到吸毒者拦路威胁要钱怎么办 开车遇见拦路要钱的怎么办 高速上有人拦车怎么办 马路上有人拦车怎么办 苹果手机下截软件要钱怎么办 孩子在学校问同学要钱怎么办 在学校被同学要钱怎么办 把人家店砸了要怎么办 外汇出金不到账怎么办 把罚款单弄丢了怎么办 在12306买不到下铺怎么办有 地铁票买反了怎么办 香港买错特惠票怎么办 到达迪拜t3 后怎么办 海藻面膜调多了怎么办 被鸡爪子抓伤了怎么办 被鸡抓伤肿了怎么办 护士电子化没有激活码怎么办 窗帘盒螺丝掉了怎么办 窗帘的环扣掉了怎么办 门式起重吊装行车脱轨怎么办 在日本丢了东西怎么办 在日本钱包丢了怎么办 被起诉后没钱还怎么办 分期付款卖车打不起车款怎么办 地铁票买多了怎么办 工伤陪护费没有发票怎么办 工伤医疗费报销单位不盖章怎么办 家里的led灯坏了怎么办 吊顶led灯坏了怎么办 客厅空了一面墙怎么办 轨道灯的轨道不够长怎么办 奔驰大灯不亮了怎么办 led顶灯不亮了怎么办 吸顶灯led灯坏了怎么办