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

照样画葫芦,用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 Flask
from flask import send_file
from flask import request
import sys
from StringIO import StringIO
import pyscreenshot

from Quartz.CoreGraphics import CGEventCreateMouseEvent
from Quartz.CoreGraphics import CGEventPost
from Quartz.CoreGraphics import kCGEventMouseMoved
from Quartz.CoreGraphics import kCGEventLeftMouseDown
from Quartz.CoreGraphics import kCGEventLeftMouseDown
from Quartz.CoreGraphics import kCGEventLeftMouseUp
from Quartz.CoreGraphics import kCGMouseButtonLeft
from Quartz.CoreGraphics import kCGHIDEventTap

app = 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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值