照样画葫芦,用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,就行动起来吧~