Re: File transfer on network
- From: MRAB <python@xxxxxxxxxxxxxxxxxxx>
- Date: Fri, 16 Jul 2010 00:08:09 +0100
mukesh tiwari wrote:
Hello allThe .recv will return an empty string only when the connection is closed
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.
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.
.
- Follow-Ups:
- Re: File transfer on network
- From: Lawrence D'Oliveiro
- Re: File transfer on network
- From: mukesh tiwari
- Re: File transfer on network
- Prev by Date: Re: multitask http server (single-process multi-connection HTTP server)
- Next by Date: Re: Code generator and visitor pattern
- Previous by thread: Re: File transfer on network
- Next by thread: Re: File transfer on network
- Index(es):
Relevant Pages
|