File transfer on network

MRAB python at mrabarnett.plus.com
Fri Jul 16 10:32:40 EDT 2010


mukesh tiwari wrote:
> On Jul 16, 4:08 am, MRAB <pyt... at mrabarnett.plus.com> wrote:
>> mukesh tiwari wrote:
>>> Hello all
>>> Currently i am trying to develop a client and server in python. Client
>>> takes screenshot in every 20 seconds and send it to server. Server
>>> store the received file in folder. Here is code for Client
>>> import sys
>>> import socket
>>> import gtk.gdk
>>> import time
>>> if __name__ == "__main__":
>>>    s=socket.socket(socket.AF_INET,socket.SOCK_STREAM);
>>>    host,port="localhost",50007
>>>    s.connect((host,port))
>>>    while 1:
>>>            w = gtk.gdk.get_default_root_window()
>>>            sz = w.get_size()
>>>            pb = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB,False,8,sz[0],sz[1])
>>>            pb = pb.get_from_drawable(w,w.get_colormap(),0,0,0,0,sz[0],sz[1])
>>>            if (pb != None):
>>>                        pb.save('/home/tmp_1/screenshot.png',"png")
>>>            f=open('/home//tmp_1/screenshot.png','rb')
>>>            while 1:
>>>                    blk=f.read(2048)
>>>                    if not blk:break
>>>                    s.send(blk)
>>>            f.close()
>>>            print 'file transfer done';
>>>            time.sleep(20)
>>>    s.close()
>>> Server Code
>>> import sys
>>> import socket
>>> import time
>>> if __name__ == "__main__":
>>>    s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
>>>    host,port="",50007
>>>    s.bind((host,port))
>>>    s.listen(1)
>>>       conn,add=s.accept()
>>>    i=0
>>>    while 1:
>>>            f=open('/home/tmp_2/copyscreenshot_'+str(i)+'.png','wb')
>>>            while 1:
>>>                    data=conn.recv(2048)
>>>                    if not data:break
>>>                    f.write(data)
>>>            f.close()
>>>            print 'file received'
>>>            time.sleep(20)
>>>            i+=1;
>>>    #print ' file received '
>>>    conn.close()
>>> My problem is that server is copying only first image
>>> copyscreenshot_0.png while my client continuously taking screen shot.
>>> Kindly tell me what is wrong with my server code.
>> The .recv will return an empty string only when the connection is closed
>> by the client, but the client keeps the connection open.
>>
>> You could either open and close a connection for each image, or have the
>> client tell the server how many bytes it's going to send, followed by
>> the bytes (my preference would be to send the size as a string ending
>> with, say, a newline).
>>
>> Another point: the .send method doesn't guarantee that it'll send all
>> the bytes (it'll return the number of bytes that it has actually sent);
>> use the .sendall method instead.
> 
> Thank you MARB. I used your first idea and opening and closing
> connection for each image. Just wanted to know if i have to take
> pictures at small intervals , this method is efficient or not?

Creating a new connection every 20 seconds should be OK. For much
smaller intervals you would need to test it yourself. The minimum
interval depends on how often you're sending data and you're sending
each time, and keeping the connection open would let you avoid the extra
overhead and get a little bit more capacity.

 > It would be great if you can provide a bit  code for second method.
> Keeping the connection open but how to know that this is end a
> particular image and save it.

The client is told how many bytes of image data it should expect, so it
can count how many it has written to the image file and close the file
when all of them.

You'll learn more if you try it yourself first! :-)



More information about the Python-list mailing list