File transfer on network

mukesh tiwari mukeshtiwari.iiitm at gmail.com
Fri Jul 16 03:52:23 EDT 2010


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? 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.



More information about the Python-list mailing list