knowledge programing

Python Screenshot Server

Thought I’d share some code python. This is just a simple way to share a screenshot over a network.

"""
Serves screen shots at a specified interval.
Doesn't handle POST requests.

Based on code from:
http://www.blendedtechnologies.com/python-trick-really-little-http-server/220

Code source:
Python Screenshot Server
""" import os import time import threading import SocketServer import SimpleHTTPServer from PIL import ImageGrab HOST = os.environ['COMPUTERNAME'] PORT = 80 REFRESH = 5 # refresh interval in seconds class CustomHandler(SimpleHTTPServer.SimpleHTTPRequestHandler): def do_GET(self): self.send_response(200) self.send_header('Refresh','%s; url=http://%s:%s/' % (REFRESH,HOST,PORT)) self.send_header('Content-type','image/jpeg') self.end_headers() f = open('screenshot.jpeg','rb') self.wfile.write(f.read()) def ScreenCapture(): while 1: ImageGrab.grab().save("screenshot.jpeg", "JPEG") time.sleep(REFRESH) ScreenCaptureThread = threading.Thread(target = ScreenCapture) ScreenCaptureThread.setDaemon(True) ScreenCaptureThread.start() httpd = SocketServer.ThreadingTCPServer((HOST, PORT),CustomHandler) print "serving at port", PORT httpd.serve_forever()