This is my first experiment with sockets. Its kinda like the beginings of a VNC app. The basic gist is that one file on computer "A" takes a screenshot of the computer screen and sends it to computer "B" where another file accepts the connection and receives the data. Computer "B" then takes the image data and, using wxpython, displays the image refreshing the picture whenever a new image is received.

My goal is to make the images on computer "B" to look like a low frame rate video (without the annoying white flash that occurs every refresh). I'll also take any tips on how to improve my networking code.

-thx

file 1:

# Client program

from time import sleep
from socket import *
from screenshot import *
IP = "localhost"
port = 10000



while 1:
    try:
        print 'sending image to port %i'%port
        s = socket(AF_INET, SOCK_STREAM)
        s.settimeout(10.0)
        s.connect((IP, port))
        s.sendall(TakeScreenShot())
        s.recv(1024)
        s.close()
        sleep(0.07)
    except Exception, e:
        print e

file 2 (screenshot.py)

# -*- coding: utf-8 -*-
from ImageGrab import grab
from StringIO import StringIO



def TakeScreenShot():
   Image = grab()
   Buffer = StringIO()
   Image.save(Buffer, format = 'JPEG')
   return Buffer.getvalue()

file 3 (Image Receiver/Viewer):

# -*- coding: utf-8 -*-

import wx
from time import sleep
from threading import Thread
from traceback import format_exc as E
from StringIO import StringIO

from socket import *


WIDTH = 1430
HEIGHT = 860

IP = "localhost"
port = 10000



class MainFrame(wx.Frame):

   def __init__(self):
      wx.Frame.__init__(self, None, title = 'Image Viewer',
                      pos = (0,0), size = (WIDTH, HEIGHT))

      self.bg = wx.Panel(self)
      
      pic = wx.EmptyBitmap(WIDTH-20, HEIGHT-60)
      
      self.ScreenShot = wx.StaticBitmap(
                              self.bg,
                              -1,
                              pic,
                              pos = (0, 0),
                              size = (WIDTH-20, HEIGHT-60)
                                             )

      
      self.screenX, self.screenY = self.ScreenShot.GetSize()

      self.Show()
      self.bg.Layout()
      self.Listen()


   def Listen(self):
      self.ListeningSocket = NewThread(port)



   def RefreshPicture( self, data ):
      self.ScreenShot.Show(False)
      stream = StringIO( data )
      img = wx.ImageFromStream(stream)
      img = img.Scale(*self.ScreenShot.GetSize())

      bitmap = wx.BitmapFromImage(img)

      self.ScreenShot.SetBitmap(bitmap)
   
      self.ScreenShot.Show(True)

   

class NewThread(Thread):
    def __init__(self, port):
        self.port = port
        Thread.__init__ ( self )
        self.start()

    def start(self):
        Thread.start(self)

        
    def run(self):
      try:
         while 1:    
            self.s = socket(AF_INET, SOCK_STREAM)
            self.s.bind((IP, self.port))
            self.s.listen(5)
            print "listening on port %i"%self.port
            # wait for client to connect
            connection, address = self.s.accept()
            while 1:
               try:
                  # buffer large enough to receive pictures
                  data = connection.recv(1048576)
                  if data:
                     print "data received"
                     window.RefreshPicture(data)
                     connection.send("received")
                     connection.close()
                     break
               except Exception, e:
                  print "socket error:", e
                  
      except Exception, e:
         print "socket error:", e
      

       
try:
   app = wx.App(redirect=False)
   window = MainFrame()
   app.MainLoop()


finally:
   del app

I don't know if it will help, but I wrote a backdoor that supports file transfer. It might help.
http://www.daniweb.com/forums/thread256651.html

I also have an updated version with two way file transfer. If your interested let me know.

Hope I could help.

Yeah that would be awesome

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.