A snap of the screen in wxPython and scp afterwards

Wednesday, August 30, 2006

This python program takes a snapshot of the screen, saving it to snap.png file, and copying the file via sftp to a server.

Python 2.4 , Wxpython and paramiko are required.


---------------------------------------------------


import wx
import paramiko

paramiko.util.log_to_file('snap.log')
filename = "snap.png"
app = wx.PySimpleApp()
p = wx.GetDisplaySize()
bitmap = wx.EmptyBitmap( p.x, p.y)
dc = wx.ScreenDC()
memdc = wx.MemoryDC()
memdc.SelectObject(bitmap)
memdc.Blit(0,0, p.x, p.y, dc, 0,0)
memdc.SelectObject(wx.NullBitmap)
bitmap.SaveFile(filename, wx.BITMAP_TYPE_PNG )
hostname = wx.GetTextFromUser("Host")
username = wx.GetTextFromUser("User Name")
password = wx.GetPasswordFromUser("Password")
port = 22

wx.MessageBox("A %s X %s snap of the screen\nis at %s" % (p.x, p.y, filename ), "Hey!")

try:
t = paramiko.Transport((hostname, port))
t.connect(username=username, password=password, hostkey=None)
sftp = paramiko.SFTPClient.from_transport(t)

data = open(filename, 'rb').read()
sftp.open(filename, 'wb').write(data)

t.close()
wx.MessageBox("The %s file was sent to server" % filename, "Hey!")

except Exception, e:
print e
try:
t.close()
except:
pass